package com.ctshk.rpc.tour.service.impl;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import com.ctshk.rpc.system.dto.*;
import com.ctshk.rpc.system.service.*;
import com.ctshk.rpc.tour.dto.schedule.*;
import com.ctshk.rpc.tour.util.Utils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.dto.strategy.StrategyRuleAmountDto;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.common.utils.Assist;
import com.ctshk.common.utils.BConst;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.StrategyRuleUtils;
import com.ctshk.rpc.guide.req.GroupDistributionAddReq;
import com.ctshk.rpc.guide.req.TeaPartyAddReq;
import com.ctshk.rpc.guide.service.IGroupDistributionService;
import com.ctshk.rpc.guide.service.ITeaPartyService;
import com.ctshk.rpc.iotc.service.IOperateBankPreferenceService;
import com.ctshk.rpc.iotc.service.IOperatePromotionService;
import com.ctshk.rpc.order.tour.dto.custom.ScheduleOrderPeopleDTO;
import com.ctshk.rpc.order.tour.req.SeriesOrderMyGroupIdQueryReq;
import com.ctshk.rpc.order.tour.service.ISeriesOrderService;
import com.ctshk.rpc.order.tour.service.ISeriesOrderTouristInfoService;
import com.ctshk.rpc.system.req.AreaIdReq;
import com.ctshk.rpc.system.req.MainDataTouristTypeListReq;
import com.ctshk.rpc.system.req.SysAgentQueryUsableListReq;
import com.ctshk.rpc.tour.dto.ScheduleDetails;
import com.ctshk.rpc.tour.dto.TripFileDTO;
import com.ctshk.rpc.tour.entity.SeriesTrip;
import com.ctshk.rpc.tour.entity.SeriesTripSchedule;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleChannelOfflineAgentCustom;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleChannelOfflineSelfCustom;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleChannelOnlineSelfPlatform;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleChannelOnlineThirdPlatform;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomAdjustTourist;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomCost;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomCostCollection;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomCostGroup;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomCostGroupAge;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomDateSubsection;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomFeeAdjust;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomSalePrice;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleCustomTouristType;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleDefaultCost;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleDefaultCostCollection;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleDefaultCostGroup;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleDefaultCostTouristType;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleDefaultFeeAdjust;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleInfoChangeRule;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleTeamRule;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleTouristType;
import com.ctshk.rpc.tour.mapper.SeriesTripMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleChannelOfflineAgentCustomMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleChannelOfflineSelfCustomMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleChannelOnlineSelfPlatformMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleChannelOnlineThirdPlatformMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleCustomCostMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleDefaultCostCollectionMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleDefaultCostGroupMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleDefaultCostMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleDefaultCostTouristTypeMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleDefaultFeeAdjustMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleInfoChangeRuleMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleTeamRuleMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleTouristTypeMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripTouristTypeMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripVisaMapper;
import com.ctshk.rpc.tour.req.AgentChannelReq;
import com.ctshk.rpc.tour.req.CollectionPlaceJson;
import com.ctshk.rpc.tour.req.CustomCostReq;
import com.ctshk.rpc.tour.req.OfficialReq;
import com.ctshk.rpc.tour.req.OtherPlatformsReq;
import com.ctshk.rpc.tour.req.OwnChannelReq;
import com.ctshk.rpc.tour.req.Passenger;
import com.ctshk.rpc.tour.req.SellingPriceReq;
import com.ctshk.rpc.tour.req.SeriesTripCustomCostGroupReq;
import com.ctshk.rpc.tour.req.SeriesTripCustomFeeAdjustReq;
import com.ctshk.rpc.tour.req.SeriesTripDefaultCostCollectionReq;
import com.ctshk.rpc.tour.req.SeriesTripDefaultCostCollectionTypeReq;
import com.ctshk.rpc.tour.req.SeriesTripDefaultCostGroupReq;
import com.ctshk.rpc.tour.req.SeriesTripDefaultFeeAdjustReq;
import com.ctshk.rpc.tour.req.SeriesTripInfoChangeRuleReq;
import com.ctshk.rpc.tour.req.SeriesTripScheduleItineraryDayReq;
import com.ctshk.rpc.tour.req.TripCustomCostReq;
import com.ctshk.rpc.tour.req.TripCustomSalePriceReq;
import com.ctshk.rpc.tour.req.TripCustomTreeDateReq;
import com.ctshk.rpc.tour.req.TripCustomTreePassengerReq;
import com.ctshk.rpc.tour.req.TripCustomTreeReq;
import com.ctshk.rpc.tour.req.TripDefaultCostReq;
import com.ctshk.rpc.tour.req.TripSupplementReq;
import com.ctshk.rpc.tour.req.es.TripCacheUpdateReq;
import com.ctshk.rpc.tour.req.schedule.AddTripScheduleReq;
import com.ctshk.rpc.tour.req.schedule.FeeAdjustSavePriceReq;
import com.ctshk.rpc.tour.req.schedule.FeeAdjustSaveReq;
import com.ctshk.rpc.tour.req.schedule.FindTripScheduleReq;
import com.ctshk.rpc.tour.req.schedule.ScheduleDepartureListQueryReq;
import com.ctshk.rpc.tour.req.schedule.SeriesAgentListByBookQueryReq;
import com.ctshk.rpc.tour.req.schedule.SeriesTripScheduleDataReq;
import com.ctshk.rpc.tour.req.schedule.SeriesTripScheduleDefaultCostCollectionReq;
import com.ctshk.rpc.tour.req.schedule.SeriesTripScheduleDefaultCostGroupReq;
import com.ctshk.rpc.tour.req.schedule.SeriesTripScheduleDefaultCostTouristTypeReq;
import com.ctshk.rpc.tour.req.schedule.SeriesTripScheduleDefaultFeeAdjustReq;
import com.ctshk.rpc.tour.req.schedule.SeriesTripScheduleInfoChangeRuleReq;
import com.ctshk.rpc.tour.req.schedule.SeriesTripScheduleSellingPriceReq;
import com.ctshk.rpc.tour.req.schedule.SeriesTripScheduleTeamRuleReq;
import com.ctshk.rpc.tour.req.schedule.TripScheduleAdjustProjectDTO;
import com.ctshk.rpc.tour.req.schedule.TripScheduleAgentReq;
import com.ctshk.rpc.tour.req.schedule.TripScheduleBookOfflineAgentFindReq;
import com.ctshk.rpc.tour.req.schedule.TripScheduleChangeRuleDTO;
import com.ctshk.rpc.tour.req.schedule.TripScheduleDefaultCostReq;
import com.ctshk.rpc.tour.req.schedule.TripScheduleListIdReq;
import com.ctshk.rpc.tour.req.schedule.TripScheduleListQueryReq;
import com.ctshk.rpc.tour.req.schedule.TripScheduleNoticeReq;
import com.ctshk.rpc.tour.req.schedule.TripSchedulePriceReq;
import com.ctshk.rpc.tour.req.schedule.TripScheduleTeaPartyReq;
import com.ctshk.rpc.tour.req.schedule.UpdateScheduleNumReq;
import com.ctshk.rpc.tour.req.schedule.UpdateSchedulePassengerReq;
import com.ctshk.rpc.tour.req.schedule.UpdateTripScheduleReq;
import com.ctshk.rpc.tour.req.schedule.UpdateTripScheduleStatusReq;
import com.ctshk.rpc.tour.req.schedule.UpdateTripStatusReq;
import com.ctshk.rpc.tour.req.schedule.VisaDetailDTO;
import com.ctshk.rpc.tour.service.ISeriesTripAppService;
import com.ctshk.rpc.tour.service.ISeriesTripScheduleDefaultCostService;
import com.ctshk.rpc.tour.service.ISeriesTripScheduleService;
import com.ctshk.rpc.visa.req.visa.TripVisaDTO;
import com.ctshk.rpc.visa.service.IVisaService;
import org.springframework.util.StringUtils;


/**
 * <p>
 * 旅行团团期 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-20
 */
@DubboService
public class SeriesTripScheduleServiceImpl extends ServiceImpl<SeriesTripScheduleMapper, SeriesTripSchedule> implements ISeriesTripScheduleService, BaseService {


    /**
     * 旅行团团期接口
     */
    @Autowired
    SeriesTripScheduleMapper seriesTripScheduleMapper;

    /**
     * 团规则接口
     */
    @Autowired
    private SeriesTripScheduleTeamRuleMapper seriesTripScheduleTeamRuleMapper;

    /**
     * 团期旅客类型规则
     */
    @Autowired
    private SeriesTripScheduleTouristTypeServiceImpl seriesTripScheduleTouristTypeService;

    /**
     * 团期--默认分摊表
     */
    @Autowired
    private SeriesTripScheduleDefaultCostMapper seriesTripScheduleDefaultCostMapper;

    /**
     * 团期--默人团综合成本
     */
    @Autowired
    private SeriesTripScheduleDefaultCostGroupMapper seriesTripScheduleDefaultCostGroupMapper;

    /**
     * 团期--默认代收费用
     */
    @Autowired
    private SeriesTripScheduleDefaultCostCollectionMapper seriesTripScheduleDefaultCostCollectionMapper;

    /**
     * 团期--默认代收费用旅客类型
     */
    @Autowired
    private SeriesTripScheduleDefaultCostTouristTypeMapper seriesTripScheduleDefaultCostTouristTypeMapper;

    /**
     * 团期--调整项目
     */
    @Autowired
    private SeriesTripScheduleDefaultFeeAdjustMapper seriesTripScheduleDefaultFeeAdjustMapper;

    /**
     * 代理
     */
    @Autowired
    private SeriesTripScheduleChannelOfflineAgentCustomMapper agentCustomMapper;

    /**
     * 自营
     */
    @Autowired
    private SeriesTripScheduleChannelOfflineSelfCustomMapper selfCustomMapper;

    /**
     * 官方平台
     */
    @Autowired
    private SeriesTripScheduleChannelOnlineSelfPlatformMapper selfPlatformMapper;

    /**
     * 第三方平台
     */
    @Autowired
    private SeriesTripScheduleChannelOnlineThirdPlatformMapper thirdPlatformMapper;

    /**
     * 行程服务
     */
    @Autowired
    private SeriesTripServiceImpl seriesTripService;

    /**
     * 自定义日期服务
     */
    @Autowired
    private SeriesTripScheduleCustomDateSubsectionServiceImpl customDateSubsectionService;

    /**
     * 自定义成本
     */
    @Autowired
    private SeriesTripScheduleCustomCostServiceImpl customCostService;

    /**
     * 调整项目年龄
     */
    @Autowired
    private SeriesTripScheduleCustomAdjustTouristServiceImpl customAdjustTouristService;

    /**
     * 调整项目
     */
    @Autowired
    private SeriesTripScheduleCustomFeeAdjustServiceImpl customFeeAdjustService;

    /**
     * 自定义团综合成本
     */
    @Autowired
    private SeriesTripScheduleCustomCostGroupServiceImpl customCostGroupService;

    /**
     * 自定义团成本自定义模板 。
     */
    @Autowired
    private SeriesTripScheduleCustomCostGroupAgeServiceImpl customCostGroupAgeService;

    /**
     * 代收费用
     */
    @Autowired
    private SeriesTripScheduleCustomCostCollectionServiceImpl customCostCollectionService;

    /**
     * 代收费用客人类型
     */
    @Autowired
    private SeriesTripScheduleCustomTouristTypeServiceImpl customTouristTypeService;

    /**
     * 自定义销售价格信息
     */
    @Autowired
    private SeriesTripScheduleCustomSalePriceServiceImpl customSalePriceService;


    @DubboReference
    private ISysDepartmentService sysDepartmentService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private SeriesTripScheduleInfoChangeRuleServiceImpl seriesTripScheduleInfoChangeRuleService;

    /**
     * 数据权限接口
     */
    @DubboReference
    private ISysPermissionService sysPermissionService;

    /**
     * 区域编码
     */
    @DubboReference
    private ISysAreaService sysAreaService;

    @DubboReference
    ISeriesOrderService seriesOrderService;

    @DubboReference
    IOperateBankPreferenceService operateBankPreferenceService;

    @DubboReference
    IOperatePromotionService operatePromotionService;

    @DubboReference
    ITeaPartyService teaPartyService;

    @DubboReference
    IGroupDistributionService groupDistributionService;

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @DubboReference
    ISeriesTripScheduleDefaultCostService seriesTripScheduleDefaultCostService;

    @Autowired
    SeriesTripMapper seriesTripMapper;

    @Autowired
    SeriesTripScheduleCustomCostMapper seriesTripScheduleCustomCostMapper;

    @DubboReference
    ISysAgentService sysAgentService;

    @Autowired
    private SeriesTripVisaMapper tripVisaMapper;
    @Autowired
    private SeriesTripScheduleInfoChangeRuleMapper tripScheduleInfoChangeRuleMapper;
    @DubboReference
    private IVisaService visaService;
    @DubboReference
    private ISeriesOrderTouristInfoService seriesOrderTouristInfoService;

    @Autowired
    SeriesTripScheduleChannelOfflineSelfCustomMapper seriesTripScheduleChannelOfflineSelfCustomMapper;

    @Autowired
    SeriesTripScheduleChannelOfflineAgentCustomMapper seriesTripScheduleChannelOfflineAgentCustomMapper;

    @Autowired
    SeriesTripTouristTypeMapper seriesTripTouristTypeMapper;

    @DubboReference
    IMainDataTouristTypeService mainDataTouristTypeService;

    @Autowired
    private ISeriesTripAppService iSeriesTripAppService;

    @Autowired
    SeriesTripScheduleTouristTypeMapper seriesTripScheduleTouristTypeMapper;

    @DubboReference
    private ISysUserService sysUserService;



    @Override
    public SeriesTripScheduleDTO getTripScheduleId(Long id) {
        //旅行团基础信息
        SeriesTripScheduleDTO seriesTripScheduleDTO = new SeriesTripScheduleDTO();
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(id);
        if (seriesTripSchedule == null) {
            SystemError error = SystemError.TOUR_ORDER_30021;
            throw new BusinessException(error);
        }
        BeanUtils.copyProperties(seriesTripSchedule, seriesTripScheduleDTO);
        SeriesTripScheduleDataDTO seriesTripScheduleDataDTO = new SeriesTripScheduleDataDTO();
        BeanUtils.copyProperties(seriesTripSchedule, seriesTripScheduleDataDTO);
        SeriesTrip seriesTrip = seriesTripMapper.assertExistById(seriesTripSchedule.getTripId());
        seriesTripScheduleDataDTO.setTitle(seriesTrip.getTitle());
        seriesTripScheduleDataDTO.setSubtitle(seriesTrip.getSubtitle());
        seriesTripScheduleDataDTO.setTripFeaturesJson(seriesTrip.getTripFeaturesJson());
        seriesTripScheduleDataDTO.setTourFeeInclude(seriesTrip.getTourFeeInclude());
        seriesTripScheduleDataDTO.setTourFeeExclude(seriesTrip.getTourFeeExclude());
        seriesTripScheduleDataDTO.setSignNotice(seriesTrip.getSignNotice());
        seriesTripScheduleDataDTO.setRemark(seriesTrip.getRemark());
        seriesTripScheduleDataDTO.setDestinationId(seriesTrip.getDestinationId());
        seriesTripScheduleDataDTO.setDestination(seriesTrip.getDestination());
        seriesTripScheduleDataDTO.setDeparturePlaceId(seriesTrip.getDeparturePlaceId());
        seriesTripScheduleDataDTO.setDeparturePlace(seriesTrip.getDeparturePlace());
        seriesTripScheduleDTO.setSeriesTripScheduleDataDTO(seriesTripScheduleDataDTO);
        seriesTripScheduleDTO.setCostSharingType(seriesTripSchedule.getCostSharingType());
        //旅行团规则列表
        SeriesTripScheduleTeamRuleDTO seriesTripScheduleTeamRuleDTO = new SeriesTripScheduleTeamRuleDTO();
        QueryWrapper<SeriesTripScheduleTeamRule> queryWrapper = new QueryWrapper<>();
        SeriesTripScheduleTeamRule seriesTripScheduleTeamRule = seriesTripScheduleTeamRuleMapper.selectOne(queryWrapper.eq("schedule_id", id));
        BeanUtils.copyProperties(seriesTripScheduleTeamRule, seriesTripScheduleTeamRuleDTO);
        //负责人
        seriesTripScheduleTeamRuleDTO.setChargePersonId(seriesTripSchedule.getChargePersonId());
        seriesTripScheduleTeamRuleDTO.setChargePersonName(seriesTripSchedule.getChargePersonName());
        seriesTripScheduleTeamRuleDTO.setChargePersonNumber(seriesTripSchedule.getChargePersonNumber());

        //操作人
        seriesTripScheduleTeamRuleDTO.setOpId(seriesTrip.getOpId());
        seriesTripScheduleTeamRuleDTO.setOpName(seriesTrip.getOpName());
        seriesTripScheduleTeamRuleDTO.setOpNumber(seriesTrip.getOpNumber());

        List<Passenger> passengerList = new ArrayList<>();
        List<SeriesTripScheduleTouristType> seriesTripScheduleTouristTypes = seriesTripScheduleTouristTypeService.list(new QueryWrapper<SeriesTripScheduleTouristType>().lambda().eq(SeriesTripScheduleTouristType::getRuleId, seriesTripScheduleTeamRule.getId()).orderByDesc(SeriesTripScheduleTouristType::getSort));
        if (CollectionUtils.isNotEmpty(seriesTripScheduleTouristTypes)) {
            for (SeriesTripScheduleTouristType seriesTripScheduleTouristType : seriesTripScheduleTouristTypes) {
                Passenger passenger = new Passenger();
                passenger.setId(seriesTripScheduleTouristType.getId());
                passenger.setMainDataId(seriesTripScheduleTouristType.getMainDataId());
                passenger.setName(seriesTripScheduleTouristType.getName());
                passenger.setIsOccupyBed(seriesTripScheduleTouristType.getIsOccupyBed());
                passenger.setAgeBegin(seriesTripScheduleTouristType.getAgeBegin());
                passenger.setAgeEnd(seriesTripScheduleTouristType.getAgeEnd());
                passenger.setSort(seriesTripScheduleTouristType.getSort());
                passengerList.add(passenger);
            }
        }
        seriesTripScheduleTeamRuleDTO.setPassenger(passengerList);

        List<SeriesTripScheduleInfoChangeRule> seriesTripScheduleInfoChangeRules = seriesTripScheduleInfoChangeRuleService.list(new QueryWrapper<SeriesTripScheduleInfoChangeRule>().lambda().eq(SeriesTripScheduleInfoChangeRule::getScheduleId, id));
        List<SeriesTripInfoChangeRuleReq> changeRuleList = EntityUtil.copyList(seriesTripScheduleInfoChangeRules, SeriesTripInfoChangeRuleReq.class);
        seriesTripScheduleTeamRuleDTO.setChangeRuleList(changeRuleList);
        seriesTripScheduleDTO.setSeriesTripScheduleTeamRuleDTO(seriesTripScheduleTeamRuleDTO);
        seriesTripScheduleDTO.setAgentSalePermissionType(seriesTripSchedule.getAgentSalePermissionType());
        seriesTripScheduleDTO.setSelfSalePermissionType(seriesTripSchedule.getSelfSalePermissionType());
        //查询自营渠道
        List<SeriesTripScheduleChannelOfflineSelfCustom> selfCustomList = selfCustomMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>().lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, id));
        List<OwnChannelReq> ownChannelList = EntityUtil.copyList(selfCustomList, OwnChannelReq.class);
        seriesTripScheduleDTO.setOwnChannelList(ownChannelList);
        //代理商
        List<SeriesTripScheduleChannelOfflineAgentCustom> agentCustomList = agentCustomMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOfflineAgentCustom>().lambda().eq(SeriesTripScheduleChannelOfflineAgentCustom::getScheduleId, id));
        List<AgentChannelReq> agentChannelList = EntityUtil.copyList(agentCustomList, AgentChannelReq.class);
        seriesTripScheduleDTO.setAgentChannelList(agentChannelList);
        //官方平台
        List<SeriesTripScheduleChannelOnlineSelfPlatform> selfPlatformList = selfPlatformMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOnlineSelfPlatform>().lambda().eq(SeriesTripScheduleChannelOnlineSelfPlatform::getScheduleId, id));
        List<OfficialReq> officialList = EntityUtil.copyList(selfPlatformList, OfficialReq.class);
        seriesTripScheduleDTO.setOfficialList(officialList);
        //第三方平台
        List<SeriesTripScheduleChannelOnlineThirdPlatform> thirdPlatformList = thirdPlatformMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOnlineThirdPlatform>().lambda().eq(SeriesTripScheduleChannelOnlineThirdPlatform::getScheduleId, id));
        List<OtherPlatformsReq> otherPlatformsList = EntityUtil.copyList(thirdPlatformList, OtherPlatformsReq.class);
        seriesTripScheduleDTO.setOtherPlatformsList(otherPlatformsList);

        if (seriesTripSchedule.getCostSharingType() == 1) {
            //成本分摊主表
            SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCostDefaultCost = seriesTripScheduleDefaultCostMapper.selectOne(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, id).eq(SeriesTripScheduleDefaultCost::getIsDeleted, IsDeletedCode.NO.getCode()));
            TripScheduleDefaultCostDTO tripScheduleDefaultCostDTO = new TripScheduleDefaultCostDTO();
            tripScheduleDefaultCostDTO.setId(seriesTripScheduleDefaultCostDefaultCost.getId());
            tripScheduleDefaultCostDTO.setTripId(seriesTripScheduleDefaultCostDefaultCost.getTripId());
            SeriesTripScheduleSellingPriceDTO seriesTripScheduleSellingPriceDTO = new SeriesTripScheduleSellingPriceDTO();
            BeanUtils.copyProperties(seriesTripScheduleDefaultCostDefaultCost, seriesTripScheduleSellingPriceDTO);
            tripScheduleDefaultCostDTO.setSeriesTripScheduleSellingPriceDTO(seriesTripScheduleSellingPriceDTO);
            //团综合成本
            List<SeriesTripScheduleDefaultCostGroup> seriesTripScheduleDefaultCostGroupList = seriesTripScheduleDefaultCostGroupMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostGroup>().lambda().eq(SeriesTripScheduleDefaultCostGroup::getIsDeleted, IsDeletedCode.NO.getCode()).eq(SeriesTripScheduleDefaultCostGroup::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()));
            if (CollectionUtils.isNotEmpty(seriesTripScheduleDefaultCostGroupList)) {
                List<SeriesTripScheduleDefaultCostGroupDTO> seriesTripDefaultCostGroupReqList = EntityUtil.copyList(seriesTripScheduleDefaultCostGroupList, SeriesTripScheduleDefaultCostGroupDTO.class);
                tripScheduleDefaultCostDTO.setCostGroupList(seriesTripDefaultCostGroupReqList);
            }
            //调整费用
            List<SeriesTripScheduleDefaultFeeAdjust> defaultFeeAdjustList = seriesTripScheduleDefaultFeeAdjustMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultFeeAdjust>().lambda().or(w -> w.eq(SeriesTripScheduleDefaultFeeAdjust::getIsDeleted, IsDeletedCode.NO.getCode()).or().isNull(SeriesTripScheduleDefaultFeeAdjust::getIsDeleted)).eq(SeriesTripScheduleDefaultFeeAdjust::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()));
            if (CollectionUtils.isNotEmpty(defaultFeeAdjustList)) {
                List<SeriesTripScheduleDefaultFeeAdjustDTO> feeAdjustList = EntityUtil.copyList(defaultFeeAdjustList, SeriesTripScheduleDefaultFeeAdjustDTO.class);
                tripScheduleDefaultCostDTO.setFeeAdjustList(feeAdjustList);
            }
            //代收费用
            List<SeriesTripScheduleDefaultCostCollection> defaultCostCollectionList = seriesTripScheduleDefaultCostCollectionMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostCollection>().lambda().or(w -> w.eq(SeriesTripScheduleDefaultCostCollection::getIsDeleted, IsDeletedCode.NO.getCode()).or().isNull(SeriesTripScheduleDefaultCostCollection::getIsDeleted)).eq(SeriesTripScheduleDefaultCostCollection::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()));
            if (CollectionUtils.isNotEmpty(defaultCostCollectionList)) {
                List<SeriesTripScheduleDefaultCostCollectionDTO> costCollectionList = new ArrayList<>();
                for (SeriesTripScheduleDefaultCostCollection seriesTripScheduleDefaultCostCollection : defaultCostCollectionList) {
                    SeriesTripScheduleDefaultCostCollectionDTO seriesTripScheduleDefaultCostCollectionDTO = new SeriesTripScheduleDefaultCostCollectionDTO();
                    BeanUtils.copyProperties(seriesTripScheduleDefaultCostCollection, seriesTripScheduleDefaultCostCollectionDTO);
                    List<SeriesTripScheduleDefaultCostTouristType> defaultCostCollectionTouristTypeList = seriesTripScheduleDefaultCostTouristTypeMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostTouristType>().lambda().or(w-> w.eq(SeriesTripScheduleDefaultCostTouristType::getIsDeleted, IsDeletedCode.NO.getCode()).or().isNull(SeriesTripScheduleDefaultCostTouristType::getIsDeleted)).eq(SeriesTripScheduleDefaultCostTouristType::getCollectionId, seriesTripScheduleDefaultCostCollection.getId()));
                    List<SeriesTripScheduleDefaultCostTouristTypeDTO> costCollectionTypeList = EntityUtil.copyList(defaultCostCollectionTouristTypeList, SeriesTripScheduleDefaultCostTouristTypeDTO.class);
                    seriesTripScheduleDefaultCostCollectionDTO.setCostCollectionTypeList(costCollectionTypeList);
                    costCollectionList.add(seriesTripScheduleDefaultCostCollectionDTO);
                }
                tripScheduleDefaultCostDTO.setCostCollectionList(costCollectionList);
            }

            seriesTripScheduleDTO.setTripScheduleDefaultCost(tripScheduleDefaultCostDTO);
        }

        if (seriesTripSchedule.getCostSharingType() == 2) {
            TripCustomTreeReq tripCustomTree = new TripCustomTreeReq();
            List<SeriesTripScheduleTouristType> seriesTripTouristTypeList = seriesTripScheduleTouristTypeService.list(new QueryWrapper<SeriesTripScheduleTouristType>().lambda().eq(SeriesTripScheduleTouristType::getScheduleId, id).isNotNull(SeriesTripScheduleTouristType::getAgeBegin).orderByDesc(SeriesTripScheduleTouristType::getSort));
            List<TripCustomTreePassengerReq> touristTypeList = EntityUtil.copyList(seriesTripTouristTypeList, TripCustomTreePassengerReq.class);
            tripCustomTree.setPassengerList(touristTypeList);
            List<SeriesTripScheduleCustomDateSubsection> seriesTripCustomDateSubsectionList = customDateSubsectionService.list(new QueryWrapper<SeriesTripScheduleCustomDateSubsection>().lambda().eq(SeriesTripScheduleCustomDateSubsection::getScheduleId, id));
            List<TripCustomTreeDateReq> dateList = EntityUtil.copyList(seriesTripCustomDateSubsectionList, TripCustomTreeDateReq.class);
            tripCustomTree.setDateList(dateList);
            seriesTripScheduleDTO.setTripCustomTree(tripCustomTree);

            //成本分摊主表
//            SeriesTripScheduleCustomCost seriesTripCustomCost = customCostService.getOne(new QueryWrapper<SeriesTripScheduleCustomCost>().lambda().eq(SeriesTripScheduleCustomCost::getScheduleId, id));
            SeriesTripScheduleCustomCost seriesTripCustomCost = seriesTripScheduleCustomCostMapper.assertExistSelectOneByKey("schedule_id", id);
            TripCustomCostReq tripCustomCostReq = null;
            tripCustomCostReq = new TripCustomCostReq();

            BeanUtils.copyProperties(seriesTripCustomCost, tripCustomCostReq);
            tripCustomCostReq.setId(seriesTripCustomCost.getId());
            tripCustomCostReq.setTripId(seriesTripCustomCost.getTripId());
            List<SeriesTripScheduleCustomSalePrice> seriesTripCustomSalePriceList = customSalePriceService.list(new QueryWrapper<SeriesTripScheduleCustomSalePrice>().lambda().eq(SeriesTripScheduleCustomSalePrice::getCostId, seriesTripCustomCost.getId()));
            List<TripCustomSalePriceReq> customSalePriceList = new ArrayList<>();
            for (SeriesTripScheduleCustomSalePrice seriesTripCustomSalePrice : seriesTripCustomSalePriceList) {
                TripCustomSalePriceReq tripCustomSalePriceReq = new TripCustomSalePriceReq();
                BeanUtils.copyProperties(seriesTripCustomSalePrice, tripCustomSalePriceReq);
                SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomSalePrice.getDateSubsectionId());
                if (seriesTripCustomDateSubsection != null) {
                    tripCustomSalePriceReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                    tripCustomSalePriceReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                    tripCustomSalePriceReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                    tripCustomSalePriceReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                    tripCustomSalePriceReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                    tripCustomSalePriceReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                    tripCustomSalePriceReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                    tripCustomSalePriceReq.setDateId(seriesTripCustomDateSubsection.getId());
                }
                SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomSalePrice.getTripTouristTypeId());
                if (seriesTripTouristType != null) {
                    tripCustomSalePriceReq.setPassengerTypeId(seriesTripTouristType.getId());
                    tripCustomSalePriceReq.setMainDataId(seriesTripTouristType.getMainDataId());
                    tripCustomSalePriceReq.setName(seriesTripTouristType.getName());
                    tripCustomSalePriceReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                    tripCustomSalePriceReq.setAgeEnd(seriesTripTouristType.getAgeEnd());
                }
                customSalePriceList.add(tripCustomSalePriceReq);
            }
            tripCustomCostReq.setCustomSalePriceList(customSalePriceList);

            //团综合成本
            List<SeriesTripScheduleCustomCostGroup> customCostGroupList = customCostGroupService.list(new QueryWrapper<SeriesTripScheduleCustomCostGroup>().lambda().eq(SeriesTripScheduleCustomCostGroup::getCostId, seriesTripCustomCost.getId()));
            if (CollectionUtils.isNotEmpty(customCostGroupList)) {
                List<SeriesTripCustomCostGroupReq> costGroupList = EntityUtil.copyList(customCostGroupList, SeriesTripCustomCostGroupReq.class);

                for (SeriesTripCustomCostGroupReq seriesTripCustomCostGroupReq : costGroupList) {
                    List<CustomCostReq> customCostReqs = new ArrayList<>();
                    List<SeriesTripScheduleCustomCostGroupAge> customCostGroupAgeList = customCostGroupAgeService.list(new QueryWrapper<SeriesTripScheduleCustomCostGroupAge>().lambda().eq(SeriesTripScheduleCustomCostGroupAge::getGroupCostId, seriesTripCustomCostGroupReq.getId()));
                    for (SeriesTripScheduleCustomCostGroupAge seriesTripCustomCostGroupAge : customCostGroupAgeList) {
                        CustomCostReq customCostReq = new CustomCostReq();
                        customCostReq.setPrice(seriesTripCustomCostGroupAge.getPrice());
                        SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomCostGroupAge.getDateSubsectionId());
                        if (seriesTripCustomDateSubsection != null) {
                            customCostReq.setDateId(seriesTripCustomDateSubsection.getId());
                            customCostReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                            customCostReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                            customCostReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                            customCostReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                            customCostReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                            customCostReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                        }
                        SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomCostGroupAge.getTripTouristTypeId());
                        if (seriesTripTouristType != null) {
                            customCostReq.setMainDataId(seriesTripTouristType.getMainDataId());
                            customCostReq.setName(seriesTripTouristType.getName());
                            customCostReq.setAgeBegin(seriesTripTouristType.getAgeEnd());
                            customCostReq.setAgeEnd(seriesTripTouristType.getAgeEnd());
                            customCostReq.setPassengerTypeId(seriesTripTouristType.getId());

                        }
                        customCostReqs.add(customCostReq);
                    }
                    seriesTripCustomCostGroupReq.setCustomCostList(customCostReqs);
                }

                tripCustomCostReq.setCostGroupList(costGroupList);
            }
            //调整费用
            List<SeriesTripScheduleCustomFeeAdjust> customFeeAdjustList = customFeeAdjustService.list(new QueryWrapper<SeriesTripScheduleCustomFeeAdjust>().lambda().eq(SeriesTripScheduleCustomFeeAdjust::getCostId, seriesTripCustomCost.getId()));
            if (CollectionUtils.isNotEmpty(customFeeAdjustList)) {
                List<SeriesTripCustomFeeAdjustReq> feeAdjustList = EntityUtil.copyList(customFeeAdjustList, SeriesTripCustomFeeAdjustReq.class);

                for (SeriesTripCustomFeeAdjustReq seriesTripCustomFeeAdjustReq : feeAdjustList) {
                    List<CustomCostReq> customCostReqs = new ArrayList<>();
                    List<SeriesTripScheduleCustomAdjustTourist> customFeeAdjustTouristTypeList = customAdjustTouristService.list(new QueryWrapper<SeriesTripScheduleCustomAdjustTourist>().lambda().eq(SeriesTripScheduleCustomAdjustTourist::getAdjustId, seriesTripCustomFeeAdjustReq.getId()));
                    for (SeriesTripScheduleCustomAdjustTourist seriesTripCustomFeeAdjustTouristType : customFeeAdjustTouristTypeList) {
                        CustomCostReq customCostReq = new CustomCostReq();
                        customCostReq.setPrice(seriesTripCustomFeeAdjustTouristType.getPrice());
                        SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomFeeAdjustTouristType.getDateSubsectionId());
                        if (seriesTripCustomDateSubsection != null) {
                            customCostReq.setDateId(seriesTripCustomDateSubsection.getId());
                            customCostReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                            customCostReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                            customCostReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                            customCostReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                            customCostReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                            customCostReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                        }

                        SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomFeeAdjustTouristType.getTripTouristTypeId());
                        if (seriesTripTouristType != null) {
                            customCostReq.setPassengerTypeId(seriesTripTouristType.getId());
                            customCostReq.setMainDataId(seriesTripTouristType.getMainDataId());
                            customCostReq.setName(seriesTripTouristType.getName());
                            customCostReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                            customCostReq.setAgeEnd(seriesTripTouristType.getAgeEnd());

                        }
                        customCostReqs.add(customCostReq);
                    }
                    seriesTripCustomFeeAdjustReq.setCustomCostList(customCostReqs);
                }

                tripCustomCostReq.setFeeAdjustList(feeAdjustList);
            }
            //代收费用
            List<SeriesTripScheduleCustomCostCollection> customCostCollectionList = customCostCollectionService.list(new QueryWrapper<SeriesTripScheduleCustomCostCollection>().lambda().eq(SeriesTripScheduleCustomCostCollection::getCostId, seriesTripCustomCost.getId()));
            if (CollectionUtils.isNotEmpty(customCostCollectionList)) {
                List<SeriesTripDefaultCostCollectionReq> costCollectionList = new ArrayList<>();
                for (SeriesTripScheduleCustomCostCollection seriesTripCustomCostCollection : customCostCollectionList) {
                    SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq = new SeriesTripDefaultCostCollectionReq();
                    BeanUtils.copyProperties(seriesTripCustomCostCollection, seriesTripDefaultCostCollectionReq);
                    List<SeriesTripScheduleCustomTouristType> customCostCollectionTouristTypeList = customTouristTypeService.list(new QueryWrapper<SeriesTripScheduleCustomTouristType>().lambda().eq(SeriesTripScheduleCustomTouristType::getCollectionId, seriesTripCustomCostCollection.getId()));
                    List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = EntityUtil.copyList(customCostCollectionTouristTypeList, SeriesTripDefaultCostCollectionTypeReq.class);
                    seriesTripDefaultCostCollectionReq.setCostCollectionTypeList(costCollectionTypeList);
                    costCollectionList.add(seriesTripDefaultCostCollectionReq);
                }
                tripCustomCostReq.setCostCollectionList(costCollectionList);
            }

            seriesTripScheduleDTO.setTripCustomCostReq(tripCustomCostReq);
        }

        //可预订余位信息
        if (seriesTripScheduleTeamRuleDTO.getMaxPeople() != null && seriesTripScheduleDataDTO.getLockedTouristNum() != null) {
            int reserveSeatNum = seriesTripScheduleTeamRuleDTO.getMaxPeople() - seriesTripScheduleDataDTO.getLockedTouristNum();
            seriesTripScheduleDTO.setReserveSeatNum(reserveSeatNum);
        }

        seriesTripScheduleDataDTO.getLockedTouristNum();
        seriesTripScheduleTeamRuleDTO.getMaxPeople();
        seriesTripScheduleTeamRuleDTO.getEnableAdditionalPosition();

        return seriesTripScheduleDTO;
    }

    /**
     * 查询线下自营预订团期详情
     *
     * @return
     */
    @Override
    public SeriesTripScheduleDTO getTripScheduleIdBookOfflineSelf(Long id, Long userId) {
        SeriesTripScheduleDTO dto = getTripScheduleId(id);

        TripScheduleDefaultCostDTO defaultCostDto = dto.getTripScheduleDefaultCost();
        if (defaultCostDto != null) {
            SeriesTripScheduleSellingPriceDTO priceDto = defaultCostDto.getSeriesTripScheduleSellingPriceDTO();
            if (priceDto != null) {
                BigDecimal price = calcSalePriceInfoOfflineSelf(dto.getId(), userId, priceDto.getGroupCustomerPrice());
                priceDto.setGroupCustomerPrice(price);
            }
        }

        TripCustomCostReq customCostDto = dto.getTripCustomCostReq();
        if (customCostDto != null) {
            List<TripCustomSalePriceReq> priceDtoList = customCostDto.getCustomSalePriceList();
            if (Assist.isNotEmpty(priceDtoList)) {
                for (TripCustomSalePriceReq priceDto : priceDtoList) {
                    BigDecimal price = calcSalePriceInfoOfflineSelf(dto.getId(), userId, priceDto.getGroupCustomerPrice());
                    priceDto.setGroupCustomerPrice(price);
                }
            }
        }
        return dto;
    }

    /**
     * 查询线下代理预订团期详情
     *
     * @return
     */
    @Override
    public SeriesTripScheduleDTO getTripScheduleIdBookOfflineAgent(TripScheduleBookOfflineAgentFindReq req) {
        SeriesTripScheduleDTO dto = getTripScheduleId(req.getId());

        TripScheduleDefaultCostDTO defaultCostDto = dto.getTripScheduleDefaultCost();
        if (defaultCostDto != null) {
            SeriesTripScheduleSellingPriceDTO priceDto = defaultCostDto.getSeriesTripScheduleSellingPriceDTO();
            if (priceDto != null) {
                BigDecimal price = calcSalePriceInfoOfflineAgent(dto.getId(), req.getAgentId(), priceDto.getGroupAgencyPrice());
                priceDto.setGroupAgencyPrice(price);
            }
        }

        TripCustomCostReq customCostDto = dto.getTripCustomCostReq();
        if (customCostDto != null) {
            List<TripCustomSalePriceReq> priceDtoList = customCostDto.getCustomSalePriceList();
            if (Assist.isNotEmpty(priceDtoList)) {
                for (TripCustomSalePriceReq priceDto : priceDtoList) {
                    BigDecimal price = calcSalePriceInfoOfflineAgent(dto.getId(), req.getAgentId(), priceDto.getGroupAgencyPrice());
                    priceDto.setGroupAgencyPrice(price);
                }
            }
        }

        return dto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateSchedulePassengerReq(UpdateSchedulePassengerReq updateSchedulePassengerReq, TokenUser tokenUser) {

        //查收已收客人数
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(updateSchedulePassengerReq.getId());
        //查询团队规则
        SeriesTripScheduleTeamRule seriesTripScheduleTeamRule = seriesTripScheduleTeamRuleMapper.selectOne(new QueryWrapper<SeriesTripScheduleTeamRule>().lambda().eq(SeriesTripScheduleTeamRule::getScheduleId, updateSchedulePassengerReq.getId()));

        if (seriesTripScheduleTeamRule != null) {
            Integer lockedTouristNum = seriesTripSchedule.getLockedTouristNum();
            Integer type = updateSchedulePassengerReq.getType();
            Integer num = lockedTouristNum + updateSchedulePassengerReq.getLockedTouristNum();
            Integer maxPeople = seriesTripScheduleTeamRule.getMaxPeople();

            //留位或立即下单，已收客人数是否大于最大收客数
            if ((type == 2 || type == 3) && num > maxPeople) {
                throw newException(SystemError.TOUR_N_ORDER);
            }

            //追位
            if (type == 1) {
                SeriesTripScheduleTeamRule scheduleTeamRule = new SeriesTripScheduleTeamRule();
                scheduleTeamRule.setId(seriesTripScheduleTeamRule.getId());
                scheduleTeamRule.setMaxPeople(num);
                seriesTripScheduleTeamRuleMapper.updateById(scheduleTeamRule);
            }
            //留位
            if (type == 2) {
                SeriesTripScheduleTeamRule scheduleTeamRule = new SeriesTripScheduleTeamRule();
                scheduleTeamRule.setId(seriesTripScheduleTeamRule.getId());
                scheduleTeamRule.setReservedSeatsNum(seriesTripScheduleTeamRule.getReservedSeatsNum() - lockedTouristNum);
                seriesTripScheduleTeamRuleMapper.updateById(scheduleTeamRule);

            }
            //立即下单
            if (type == 3) {

            }

            //更新团期
            SeriesTripSchedule tripSchedule = newUpdateEntity(SeriesTripSchedule.class, null, tokenUser);
            tripSchedule.setId(updateSchedulePassengerReq.getId());
            tripSchedule.setLockedTouristNum(num);

            //未成团且设置自动成团时，判断收客数是否已达最小成团数，则成团	
            if (BConst.ONE.equals(seriesTripSchedule.getScheduleStatus())
                    && BConst.ZERO.equals(seriesTripScheduleTeamRule.getEnableAutoGroup()) && num >= seriesTripScheduleTeamRule.getMinPeople()) {
                tripSchedule.setScheduleStatus(BConst.TWO);
            }

            seriesTripScheduleMapper.updateById(tripSchedule);

            // 更新缓存
            CompletableFuture.runAsync(() -> {
                iSeriesTripAppService.updateTripCache(new TripCacheUpdateReq().setIds(Arrays.asList(seriesTripSchedule.getTripId())));
            });
        }
        return new Result();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSchedule(List<AddTripScheduleReq> addTripScheduleReqList, TokenUser tokenUser) {

        for (AddTripScheduleReq addTripScheduleReq : addTripScheduleReqList) {
            List<LocalDate> list = addTripScheduleReq.getDateList();
            if (CollectionUtils.isEmpty(list)) {
                throw new BusinessException(SystemError.SYS_409, "dateList");
            }

            //查询旅客类型主数据列表
            Map<Long, MainDataTouristTypeListDTO> mainDataTouristTypeMap = queryMainDataTouristTypeList();
            //行程
            SeriesTrip seriesTrip = seriesTripMapper.assertExistById(addTripScheduleReq.getTripId());

            for (LocalDate localDate : list) {
                //todo  团号生成规则
                //新增团期基本资料
                Long scheduleId = IdWorker.getId();
                //c成本分摊类型（1：默认 2：自定义）
                Integer costSharingType = addTripScheduleReq.getCostSharingType();
                Long tripId = addTripScheduleReq.getTripId();
                SeriesTripSchedule seriesTripSchedule = newCreateEntity(SeriesTripSchedule.class, null, tokenUser);
                SeriesTripScheduleDataReq seriesTripScheduleDataReq = addTripScheduleReq.getSeriesTripScheduleDataReq();
                //團號生成規則為：行程碼前三位字母+部門代碼+年後2位+目的地所對應的區域代碼（主數據【國家/城市】）+旅遊天數3位+出發月份日期4位+A/B/C…/Z（例如：同一行程同一天有兩個團，則第一個為A，第二個為B，如此類推）
                //部门代码
                SysDepartmentDTO sysDepartmentDTO = sysDepartmentService.queryByUserId(tokenUser.getId());

                //目的地
                AreaIdReq areaIdReq = new AreaIdReq();
                areaIdReq.setId(seriesTripScheduleDataReq.getDestinationId());
                AreaChildrenDTO areaChildrenDTO = sysAreaService.queryAreaChildren(areaIdReq);
                //天数3位
                String day = String.format("%03d", seriesTripScheduleDataReq.getTravelDays());
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                List<SeriesTripSchedule> seriesTripScheduleList = seriesTripScheduleMapper.selectList(new QueryWrapper<SeriesTripSchedule>().lambda().eq(SeriesTripSchedule::getTripId, tripId).eq(SeriesTripSchedule::getDepartureDate, localDate));
                String maxSortKey = "A";
                if (CollectionUtils.isNotEmpty(seriesTripScheduleList)) {
                    List<String> stringList = new ArrayList<>();
                    for (SeriesTripSchedule tripSchedule : seriesTripScheduleList) {
                        stringList.add(tripSchedule.getSortKey());
                    }
                    String max = Collections.max(stringList);
                    int maxSort = max.charAt(0) + 1;
                    maxSortKey = Character.toString((char) maxSort);
                }
                String tripCodeAbbr = Assist.substr(seriesTrip.getTripCode(), 3);
                String tripNumber = tripCodeAbbr + sysDepartmentDTO.getDeptCode() + (year % 100) + areaChildrenDTO.getCode() + day + String.format("%02d", localDate.getMonthValue()) + String.format("%02d", localDate.getDayOfMonth()) + maxSortKey;
                BeanUtils.copyProperties(seriesTripScheduleDataReq, seriesTripSchedule);
                SeriesTripScheduleTeamRuleReq seriesTripScheduleTeamRuleReq = addTripScheduleReq.getSeriesTripScheduleTeamRuleReq();
                seriesTripSchedule.setChargePersonId(seriesTripScheduleTeamRuleReq.getChargePersonId());
                seriesTripSchedule.setChargePersonNumber(seriesTripScheduleTeamRuleReq.getChargePersonNumber());
                seriesTripSchedule.setChargePersonName(seriesTripScheduleTeamRuleReq.getChargePersonName());
                seriesTripSchedule.setOpId(seriesTripScheduleTeamRuleReq.getOpId());
                seriesTripSchedule.setOpNumber(seriesTripScheduleTeamRuleReq.getOpNumber());
                seriesTripSchedule.setOpName(seriesTripScheduleTeamRuleReq.getOpName());
                seriesTripSchedule.setLockedTouristNum(0);
                seriesTripSchedule.setTripNumber(tripNumber);
                seriesTripSchedule.setId(scheduleId);
                seriesTripSchedule.setCostSharingType(costSharingType);
                seriesTripSchedule.setDepartureDate(localDate);
                seriesTripSchedule.setBackDate(localDate.plusDays(seriesTripScheduleDataReq.getTravelDays()));
                seriesTripSchedule.setTripId(tripId);
                seriesTripSchedule.setSortKey(maxSortKey);
                seriesTripSchedule.setAgentSalePermissionType(addTripScheduleReq.getAgentSalePermissionType());
                seriesTripSchedule.setSelfSalePermissionType(addTripScheduleReq.getSelfSalePermissionType());
                seriesTripScheduleMapper.insert(seriesTripSchedule);

                //自营渠道
                List<OwnChannelReq> ownChannelList = addTripScheduleReq.getOwnChannelList();
                if (CollectionUtils.isNotEmpty(ownChannelList)) {

                    List<SeriesTripScheduleChannelOfflineSelfCustom> selfCustomList = EntityUtil.copyList(ownChannelList, SeriesTripScheduleChannelOfflineSelfCustom.class);
                    fillCreateEntity(selfCustomList, tokenUser);
                    selfCustomList.forEach(selfCustom -> {
                        selfCustom.setScheduleId(scheduleId);
                        if (selfCustom.getAdjustType() == null || selfCustom.getAdjustType() == 2) {
                            selfCustom.setAdjustType(0);
                        }
                        if(StringUtils.isEmpty(selfCustom.getAmount()) ){
                            selfCustom.setAmount(BigDecimal.ZERO);
                        }
                    });
                    selfCustomMapper.insertBatchSomeColumn(selfCustomList);
                }
                //代理商
                List<AgentChannelReq> agentChannelList = addTripScheduleReq.getAgentChannelList();
                if (CollectionUtils.isNotEmpty(agentChannelList)) {
                    List<SeriesTripScheduleChannelOfflineAgentCustom> agentCustomList = EntityUtil.copyList(agentChannelList, SeriesTripScheduleChannelOfflineAgentCustom.class);
                    fillCreateEntity(agentCustomList, tokenUser);
                    agentCustomList.forEach(agentCustom -> {
                        agentCustom.setScheduleId(scheduleId);
                        if (agentCustom.getAdjustType() == null || agentCustom.getAdjustType() == 2) {
                            agentCustom.setAdjustType(0);
                        }
                        if(StringUtils.isEmpty(agentCustom.getAmount()) ){
                            agentCustom.setAmount(BigDecimal.ZERO);
                        }
                    });
                    agentCustomMapper.insertBatchSomeColumn(agentCustomList);
                }
                //官方平台
                List<OfficialReq> officialList = addTripScheduleReq.getOfficialList();
                if (CollectionUtils.isNotEmpty(officialList)) {
                    List<SeriesTripScheduleChannelOnlineSelfPlatform> selfPlatformList = EntityUtil.copyList(officialList, SeriesTripScheduleChannelOnlineSelfPlatform.class);
                    fillCreateEntity(selfPlatformList, tokenUser);
                    selfPlatformList.forEach(selfPlatform -> {
                        selfPlatform.setScheduleId(scheduleId);
                        if (selfPlatform.getAdjustType() == null || selfPlatform.getAdjustType() == 2) {
                            selfPlatform.setAdjustType(0);
                        }
                        if(StringUtils.isEmpty(selfPlatform.getAmount()) ){
                            selfPlatform.setAmount(BigDecimal.ZERO);
                        }
                    });
                    selfPlatformMapper.insertBatchSomeColumn(selfPlatformList);
                }
                //第三方平台
                List<OtherPlatformsReq> otherPlatformsList = addTripScheduleReq.getOtherPlatformsList();
                if (CollectionUtils.isNotEmpty(otherPlatformsList)) {
                    List<SeriesTripScheduleChannelOnlineThirdPlatform> thirdPlatformList = EntityUtil.copyList(addTripScheduleReq.getOtherPlatformsList(), SeriesTripScheduleChannelOnlineThirdPlatform.class);
                    fillCreateEntity(thirdPlatformList, tokenUser);
                    thirdPlatformList.forEach(thirdPlatform -> {
                        thirdPlatform.setScheduleId(scheduleId);
                        if (thirdPlatform.getAdjustType() == null || thirdPlatform.getAdjustType() == 2) {
                            thirdPlatform.setAdjustType(0);
                        }
                        if(StringUtils.isEmpty(thirdPlatform.getAmount()) ){
                            thirdPlatform.setAmount(BigDecimal.ZERO);
                        }
                    });
                    thirdPlatformMapper.insertBatchSomeColumn(thirdPlatformList);
                }

                List<Passenger> passengerList = seriesTripScheduleTeamRuleReq.getPassenger();


                //新增团期团队规则
                SeriesTripScheduleTeamRule seriesTripScheduleTeamRule = new SeriesTripScheduleTeamRule();
                BeanUtils.copyProperties(seriesTripScheduleTeamRuleReq, seriesTripScheduleTeamRule);
                seriesTripScheduleTeamRule.setScheduleId(scheduleId);
                seriesTripScheduleTeamRuleMapper.insert(seriesTripScheduleTeamRule);

                Long ruleId = seriesTripScheduleTeamRule.getId();
                if (CollectionUtils.isNotEmpty(passengerList)) {
                    List<SeriesTripScheduleTouristType> tripTouristTypeList = new ArrayList();
                    SeriesTripScheduleTouristType seriesTripTouristType = null;

                    for (Passenger passenger : passengerList) {
                        seriesTripTouristType = newCreateEntity(SeriesTripScheduleTouristType.class, null, tokenUser);
                        seriesTripTouristType.setTripId(tripId);
                        seriesTripTouristType.setRuleId(ruleId);
                        seriesTripTouristType.setScheduleId(scheduleId);
                        seriesTripTouristType.setMainDataId(passenger.getId());
                        seriesTripTouristType.setName(passenger.getName());
                        seriesTripTouristType.setAgeBegin(passenger.getAgeBegin());
                        seriesTripTouristType.setAgeEnd(passenger.getAgeEnd());
                        seriesTripTouristType.setSort(passenger.getSort());
                        seriesTripTouristType.setIsOccupyBed(passenger.getIsOccupyBed());

                        MainDataTouristTypeListDTO mainDataTouristType = mainDataTouristTypeMap.get(seriesTripTouristType.getMainDataId());
                        if (mainDataTouristType != null) {
                            seriesTripTouristType.setAgeBegin(mainDataTouristType.getAgeBegin());
                            seriesTripTouristType.setAgeEnd(mainDataTouristType.getAgeEnd());
                        }
                        tripTouristTypeList.add(seriesTripTouristType);
                    }
                    seriesTripScheduleTouristTypeService.saveBatch(tripTouristTypeList);
                }

                List<SeriesTripScheduleInfoChangeRuleReq> ruleList = seriesTripScheduleTeamRuleReq.getChangeRuleList();
                if (CollectionUtils.isNotEmpty(ruleList)) {
                    List<SeriesTripScheduleInfoChangeRule> seriesTripInfoChangeRuleList = new ArrayList<>();
                    SeriesTripScheduleInfoChangeRule seriesTripInfoChangeRule = null;
                    for (SeriesTripScheduleInfoChangeRuleReq seriesTripScheduleInfoChangeRuleReq : ruleList) {
                        seriesTripInfoChangeRule = newCreateEntity(SeriesTripScheduleInfoChangeRule.class, null, tokenUser);
                        seriesTripInfoChangeRule.setTripId(tripId);
                        seriesTripInfoChangeRule.setGroupRuleId(ruleId);
                        seriesTripInfoChangeRule.setScheduleId(scheduleId);
                        seriesTripInfoChangeRule.setAmount(seriesTripScheduleInfoChangeRuleReq.getAmount());
                        seriesTripInfoChangeRule.setMainDataId(seriesTripScheduleInfoChangeRuleReq.getMainDataId());
                        seriesTripInfoChangeRule.setName(seriesTripScheduleInfoChangeRuleReq.getName());
                        seriesTripInfoChangeRuleList.add(seriesTripInfoChangeRule);
                    }
                    seriesTripScheduleInfoChangeRuleService.saveBatch(seriesTripInfoChangeRuleList);
                }

                if (costSharingType == 1) {
                    tripScheduleDefaultCost(addTripScheduleReq, scheduleId, mainDataTouristTypeMap);
                }
                if (costSharingType == 2) {
                    tripScheduleCustomCost(addTripScheduleReq, scheduleId);
                }
            }
        }

        // 更新缓存
        CompletableFuture.runAsync(() -> {
            Set<Long> tripIds = addTripScheduleReqList.stream().map(item -> item.getTripId()).collect(Collectors.toSet());
            iSeriesTripAppService.updateTripCache(new TripCacheUpdateReq().setIds(new ArrayList<>(tripIds)));
        });

    }


    @Override
    public List<TripScheduleListDTO> getTripScheduleList(TripScheduleListIdReq tripScheduleListIdReq) {
        return seriesTripScheduleMapper.getTripScheduleList(tripScheduleListIdReq.getListId());
    }

    @Override
    public PageResponse<TripScheduleDTO> scheduleList(TripScheduleListQueryReq tripScheduleListQueryReq, TokenUser tokenUser) {
        return scheduleListHandle(tripScheduleListQueryReq, tokenUser, true);
    }

    /**
     * 运营中心团期管理列表查询
     */
    @Override
    public PageResponse<TripScheduleDTO> scheduleListForIotc(TripScheduleListQueryReq tripScheduleListQueryReq, TokenUser tokenUser) {
        return scheduleListHandle(tripScheduleListQueryReq, tokenUser, false);
    }

    /**
     * 查询团期列表逻辑处理
     *
     * @param tripScheduleListQueryReq
     * @param tokenUser
     * @param isValidatePermission     是否验证权限
     * @return
     */
    private PageResponse<TripScheduleDTO> scheduleListHandle(TripScheduleListQueryReq tripScheduleListQueryReq, TokenUser tokenUser, boolean isValidatePermission) {
        //验证权限
        if (isValidatePermission) {
            Result<List<Long>> permission = sysPermissionService.queryPermission(tripScheduleListQueryReq.getMenuId(), tokenUser.getId());

            if (!permission.isSuccess() || CollectionUtils.isEmpty(permission.getData())) {
                SystemError error = SystemError.USER_1001;
                throw new BusinessException(error);
            }

            List<Long> userList = permission.getData();
            tripScheduleListQueryReq.setUserList(userList);
        }
        IPage<SeriesTripSchedule> page = new Page<>(tripScheduleListQueryReq.getPageNo(), tripScheduleListQueryReq.getPageSize());
        IPage<SeriesTripSchedule> iPage = seriesTripScheduleMapper.scheduleList(page, tripScheduleListQueryReq, false);
        List<SeriesTripSchedule> seriesTripScheduleList = iPage.getRecords();
        List<TripScheduleDTO> scheduleDTOList = EntityUtil.copyList(seriesTripScheduleList, TripScheduleDTO.class);

        for (TripScheduleDTO tripScheduleDTO : scheduleDTOList) {

            JSONArray jsonArray = JSONArray.parseArray(tripScheduleDTO.getCollectionPlaceJsons());
            List<CollectionPlaceJson> collectionPlaceJson = JSONObject.parseArray(jsonArray.toJSONString(), CollectionPlaceJson.class);
            tripScheduleDTO.setCollectionPlaceJson(collectionPlaceJson);
            JSONArray jsonArrayDissolution = JSONArray.parseArray(tripScheduleDTO.getCollectionPlaceJsons());
            List<CollectionPlaceJson> dissolutionAreaJson = JSONObject.parseArray(jsonArrayDissolution.toJSONString(), CollectionPlaceJson.class);
            tripScheduleDTO.setDissolutionAreaJson(dissolutionAreaJson);

            Integer costSharingType = tripScheduleDTO.getCostSharingType();
            LocalDate departureDate = tripScheduleDTO.getDepartureDate();
            if (departureDate != null) {
                DayOfWeek dayOfWeek = departureDate.getDayOfWeek();
                tripScheduleDTO.setDepartureDayOfWeek(dayOfWeek);
            }
            LocalDate backDate = tripScheduleDTO.getBackDate();
            if (backDate != null) {
                DayOfWeek dayOfWeek = departureDate.getDayOfWeek();
                tripScheduleDTO.setBackDayOfWeek(dayOfWeek);
            }
//            UserDTO userDto = (UserDTO) redisTemplate.opsForValue().get(RedisConstants.ACCOUNT_QUERY_KEY + tripScheduleDTO.getChargePersonId());
//            if (userDto!=null&& StringUtils.isNotBlank(userDto.getDept())){
//                tripScheduleDTO.setChargePersonDepartment(userDto.getDept());
//            }
//            UserDTO opDto = (UserDTO) redisTemplate.opsForValue().get(RedisConstants.ACCOUNT_QUERY_KEY + tripScheduleDTO.getOpId());
//            if (opDto!=null&&StringUtils.isNotBlank(opDto.getDept())){
//                tripScheduleDTO.setOpDepartment(opDto.getDept());
//            }

            if (costSharingType == 1) {
                List<SeriesTripScheduleDefaultCost> defaultCostList = seriesTripScheduleDefaultCostMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, tripScheduleDTO.getId()));
                if (CollectionUtils.isNotEmpty(defaultCostList)) {
                    SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCost = defaultCostList.get(0);
                    if (seriesTripScheduleDefaultCost != null) {
                        tripScheduleDTO.setGroupCustomerPrice(seriesTripScheduleDefaultCost.getGroupCustomerPrice());

                        tripScheduleDTO.setCustomerGrossProfitRate(seriesTripScheduleDefaultCost.getCustomerGrossProfitRate());
                        tripScheduleDTO.setGroupAgencyPrice(seriesTripScheduleDefaultCost.getGroupAgencyPrice());
                        tripScheduleDTO.setAgencyGrossProfitRate(seriesTripScheduleDefaultCost.getAgencyGrossProfitRate());
                    }
                }
            }
            if (costSharingType == 2) {
                List<SeriesTripScheduleCustomSalePrice> seriesTripScheduleCustomSalePrices = customSalePriceService.list(new QueryWrapper<SeriesTripScheduleCustomSalePrice>().lambda().eq(SeriesTripScheduleCustomSalePrice::getScheduleId, tripScheduleDTO.getId()));
                if (CollectionUtils.isNotEmpty(seriesTripScheduleCustomSalePrices)) {
                    SeriesTripScheduleCustomSalePrice seriesTripScheduleCustomSalePrice = seriesTripScheduleCustomSalePrices.get(0);
                    if (seriesTripScheduleCustomSalePrice != null) {
                        tripScheduleDTO.setGroupCustomerPrice(seriesTripScheduleCustomSalePrice.getGroupCustomerPrice());
                        tripScheduleDTO.setCustomerGrossProfitRate(seriesTripScheduleCustomSalePrice.getCustomerGrossProfitRate());
                        tripScheduleDTO.setGroupAgencyPrice(seriesTripScheduleCustomSalePrice.getGroupAgencyPrice());
                        tripScheduleDTO.setAgencyGrossProfitRate(seriesTripScheduleCustomSalePrice.getAgencyGrossProfitRate());
                    }
                }
            }
        }

        return new PageResponse<>(scheduleDTOList, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSchedule(UpdateTripScheduleReq updateTripScheduleReq, TokenUser tokenUser) {
        SeriesTripScheduleDataReq seriesTripScheduleDataReq = updateTripScheduleReq.getSeriesTripScheduleDataReq();
        //新增团期基本资料
        Long scheduleId = seriesTripScheduleDataReq.getId();
        //c成本分摊类型（1：默认 2：自定义）
        Integer costSharingType = updateTripScheduleReq.getCostSharingType();
        SeriesTripSchedule seriesTripSchedule = newUpdateEntity(SeriesTripSchedule.class, seriesTripScheduleDataReq, tokenUser);

        SeriesTripScheduleTeamRuleReq seriesTripScheduleTeamRuleReq = updateTripScheduleReq.getSeriesTripScheduleTeamRuleReq();
        seriesTripSchedule.setChargePersonId(seriesTripScheduleTeamRuleReq.getChargePersonId());
        seriesTripSchedule.setChargePersonNumber(seriesTripScheduleTeamRuleReq.getChargePersonNumber());
        seriesTripSchedule.setChargePersonName(seriesTripScheduleTeamRuleReq.getChargePersonName());
        seriesTripSchedule.setOpId(seriesTripScheduleTeamRuleReq.getOpId());
        seriesTripSchedule.setOpNumber(seriesTripScheduleTeamRuleReq.getOpNumber());
        seriesTripSchedule.setOpName(seriesTripScheduleTeamRuleReq.getOpName());
        seriesTripSchedule.setId(scheduleId);
        seriesTripSchedule.setCostSharingType(costSharingType);
        seriesTripSchedule.setAgentSalePermissionType(updateTripScheduleReq.getAgentSalePermissionType());
        seriesTripSchedule.setSelfSalePermissionType(updateTripScheduleReq.getSelfSalePermissionType());
        seriesTripScheduleMapper.updateById(seriesTripSchedule);

        selfCustomMapper.delete(new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>().lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, scheduleId));
        //自营渠道
        List<OwnChannelReq> ownChannelList = updateTripScheduleReq.getOwnChannelList();
        if (CollectionUtils.isNotEmpty(ownChannelList)) {

            List<SeriesTripScheduleChannelOfflineSelfCustom> selfCustomList = EntityUtil.copyList(ownChannelList, SeriesTripScheduleChannelOfflineSelfCustom.class);
            fillCreateEntity(selfCustomList, tokenUser);
            selfCustomList.forEach(selfCustom ->{
                selfCustom.setScheduleId(scheduleId);
                if(StringUtils.isEmpty(selfCustom.getAmount())){
                    selfCustom.setAmount(BigDecimal.ZERO);
                }
            });
            selfCustomMapper.insertBatchSomeColumn(selfCustomList);
        }

        agentCustomMapper.delete(new QueryWrapper<SeriesTripScheduleChannelOfflineAgentCustom>().lambda().eq(SeriesTripScheduleChannelOfflineAgentCustom::getScheduleId, scheduleId));
        //代理商
        List<AgentChannelReq> agentChannelList = updateTripScheduleReq.getAgentChannelList();
        if (CollectionUtils.isNotEmpty(agentChannelList)) {
            List<SeriesTripScheduleChannelOfflineAgentCustom> agentCustomList = EntityUtil.copyList(agentChannelList, SeriesTripScheduleChannelOfflineAgentCustom.class);
            fillCreateEntity(agentCustomList, tokenUser);
            agentCustomList.forEach(agentCustom -> {
                agentCustom.setScheduleId(scheduleId);
                if(StringUtils.isEmpty(agentCustom.getAmount())){
                    agentCustom.setAmount(BigDecimal.ZERO);
                }
            });

            agentCustomMapper.insertBatchSomeColumn(agentCustomList);
        }
        selfPlatformMapper.delete(new QueryWrapper<SeriesTripScheduleChannelOnlineSelfPlatform>().lambda().eq(SeriesTripScheduleChannelOnlineSelfPlatform::getScheduleId, scheduleId));
        //官方平台
        List<OfficialReq> officialList = updateTripScheduleReq.getOfficialList();
        if (CollectionUtils.isNotEmpty(officialList)) {
            List<SeriesTripScheduleChannelOnlineSelfPlatform> selfPlatformList = EntityUtil.copyList(officialList, SeriesTripScheduleChannelOnlineSelfPlatform.class);
            fillCreateEntity(selfPlatformList, tokenUser);
            selfPlatformList.forEach(selfPlatform -> {
                selfPlatform.setScheduleId(scheduleId);
                if(StringUtils.isEmpty(selfPlatform.getAmount())){
                    selfPlatform.setAmount(BigDecimal.ZERO);
                }
            });
            selfPlatformMapper.insertBatchSomeColumn(selfPlatformList);
        }
        thirdPlatformMapper.delete(new QueryWrapper<SeriesTripScheduleChannelOnlineThirdPlatform>().lambda().eq(SeriesTripScheduleChannelOnlineThirdPlatform::getScheduleId, scheduleId));
        //第三方平台
        List<OtherPlatformsReq> otherPlatformsList = updateTripScheduleReq.getOtherPlatformsList();
        if (CollectionUtils.isNotEmpty(otherPlatformsList)) {
            List<SeriesTripScheduleChannelOnlineThirdPlatform> thirdPlatformList = EntityUtil.copyList(updateTripScheduleReq.getOtherPlatformsList(), SeriesTripScheduleChannelOnlineThirdPlatform.class);
            fillCreateEntity(thirdPlatformList, tokenUser);
            thirdPlatformList.forEach(thirdPlatform -> {
                thirdPlatform.setScheduleId(scheduleId);
                if(StringUtils.isEmpty(thirdPlatform.getAmount())){
                    thirdPlatform.setAmount(BigDecimal.ZERO);
                }
            });
            thirdPlatformMapper.insertBatchSomeColumn(thirdPlatformList);
        }
        //更改资料收费规则
        List<SeriesTripScheduleInfoChangeRuleReq> ruleList = seriesTripScheduleTeamRuleReq.getChangeRuleList();
        if (CollectionUtils.isNotEmpty(ruleList)) {
            tripScheduleInfoChangeRuleMapper.cleanByTrip(updateTripScheduleReq.getTripId(), seriesTripScheduleTeamRuleReq.getId(), tokenUser.getId());
            List<SeriesTripScheduleInfoChangeRule> seriesTripInfoChangeRuleList = new ArrayList<>();
            SeriesTripScheduleInfoChangeRule seriesTripInfoChangeRule = null;
            for (SeriesTripScheduleInfoChangeRuleReq seriesTripScheduleInfoChangeRuleReq : ruleList) {
                seriesTripInfoChangeRule = newCreateEntity(SeriesTripScheduleInfoChangeRule.class, null, tokenUser);
                seriesTripInfoChangeRule.setTripId(updateTripScheduleReq.getTripId());
                seriesTripInfoChangeRule.setGroupRuleId(seriesTripScheduleTeamRuleReq.getId());
                seriesTripInfoChangeRule.setScheduleId(scheduleId);
                seriesTripInfoChangeRule.setAmount(seriesTripScheduleInfoChangeRuleReq.getAmount());
                seriesTripInfoChangeRule.setMainDataId(seriesTripScheduleInfoChangeRuleReq.getMainDataId());
                seriesTripInfoChangeRule.setName(seriesTripScheduleInfoChangeRuleReq.getName());
                seriesTripInfoChangeRuleList.add(seriesTripInfoChangeRule);
            }
            if (seriesTripInfoChangeRuleList.size() > 0) {
                seriesTripScheduleInfoChangeRuleService.saveBatch(seriesTripInfoChangeRuleList);
            }
        }

        //新增团期团队规则
        SeriesTripScheduleTeamRule seriesTripScheduleTeamRule = new SeriesTripScheduleTeamRule();
        BeanUtils.copyProperties(seriesTripScheduleTeamRuleReq, seriesTripScheduleTeamRule);
        seriesTripScheduleTeamRule.setScheduleId(scheduleId);
        seriesTripScheduleTeamRuleMapper.updateById(seriesTripScheduleTeamRule);

        //旅客类型
        List<Passenger> passengerList = seriesTripScheduleTeamRuleReq.getPassenger();
        if (Assist.isNotEmpty(passengerList)) {
            //"schedule_id", scheduleId
            QueryWrapper<SeriesTripScheduleTouristType> qwnp = new QueryWrapper<SeriesTripScheduleTouristType>();
            qwnp.lambda().eq(SeriesTripScheduleTouristType::getScheduleId, scheduleId);
            qwnp.orderByDesc("sort");
            List<SeriesTripScheduleTouristType> touristTypeList = seriesTripScheduleTouristTypeMapper.selectList(qwnp);
            if (Assist.isNotEmpty(touristTypeList)) {
                for (SeriesTripScheduleTouristType touristType : touristTypeList) {
                    for (Passenger passenger : passengerList) {
                        if (Assist.equals(passenger.getMainDataId(), touristType.getMainDataId())) {
                            touristType.setIsOccupyBed(passenger.getIsOccupyBed());
                            seriesTripScheduleTouristTypeMapper.updateById(touristType);
                        }
                    }

                }
            }
        }

        if (costSharingType == 1) {
            tripScheduleDefaultCostUpdate(updateTripScheduleReq, scheduleId, tokenUser);
        }

        if (costSharingType == 2) {
            AddTripScheduleReq addTripScheduleReq = new AddTripScheduleReq();
            BeanUtils.copyProperties(updateTripScheduleReq, addTripScheduleReq);
            customCostService.remove(new QueryWrapper<SeriesTripScheduleCustomCost>().lambda().eq(SeriesTripScheduleCustomCost::getScheduleId, scheduleId));
            customSalePriceService.remove(new QueryWrapper<SeriesTripScheduleCustomSalePrice>().lambda().eq(SeriesTripScheduleCustomSalePrice::getScheduleId, scheduleId));
            customCostCollectionService.remove(new QueryWrapper<SeriesTripScheduleCustomCostCollection>().lambda().eq(SeriesTripScheduleCustomCostCollection::getScheduleId, scheduleId));
            customTouristTypeService.remove(new QueryWrapper<SeriesTripScheduleCustomTouristType>().lambda().eq(SeriesTripScheduleCustomTouristType::getScheduleId, scheduleId));
            customCostGroupService.remove(new QueryWrapper<SeriesTripScheduleCustomCostGroup>().lambda().eq(SeriesTripScheduleCustomCostGroup::getScheduleId, scheduleId));
            customCostGroupAgeService.remove(new QueryWrapper<SeriesTripScheduleCustomCostGroupAge>().lambda().eq(SeriesTripScheduleCustomCostGroupAge::getScheduleId, scheduleId));
            customFeeAdjustService.remove(new QueryWrapper<SeriesTripScheduleCustomFeeAdjust>().lambda().eq(SeriesTripScheduleCustomFeeAdjust::getScheduleId, scheduleId));
            customAdjustTouristService.remove(new QueryWrapper<SeriesTripScheduleCustomAdjustTourist>().lambda().eq(SeriesTripScheduleCustomAdjustTourist::getScheduleId, scheduleId));
            tripScheduleCustomCost(addTripScheduleReq, scheduleId);
        }

        // 更新缓存
        CompletableFuture.runAsync(() -> {
            iSeriesTripAppService.updateTripCache(new TripCacheUpdateReq().setIds(Arrays.asList(updateTripScheduleReq.getTripId())));
        });
    }

    @Override
    public TripScheduleCostDTO getCost(Long scheduleId) {

        TripScheduleCostDTO tripScheduleCostDTO = new TripScheduleCostDTO();
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(scheduleId);
        if (null == seriesTripSchedule) {
            throw new BusinessException(SystemError.SYS_411);
        }

        Integer costSharingType = seriesTripSchedule.getCostSharingType();
        tripScheduleCostDTO.setCostSharingType(costSharingType);
        tripScheduleCostDTO.setScheduleId(seriesTripSchedule.getId());

        //默认成本分摊
        if (costSharingType == 1) {
            //成本分摊主表
            List<SeriesTripScheduleDefaultCost> defaultCostList = seriesTripScheduleDefaultCostMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, scheduleId).eq(SeriesTripScheduleDefaultCost::getIsDeleted, 0));
            List<TripDefaultCostReq> tripDefaultCostReqList = new ArrayList<>();
            TripDefaultCostReq tripDefaultCostReq = null;
            for (SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCost : defaultCostList) {
                tripDefaultCostReq = new TripDefaultCostReq();
                tripDefaultCostReq.setId(seriesTripScheduleDefaultCost.getId());
                tripDefaultCostReq.setTripId(seriesTripScheduleDefaultCost.getTripId());
                SellingPriceReq sellingPriceReq = new SellingPriceReq();
                BeanUtils.copyProperties(seriesTripScheduleDefaultCost, sellingPriceReq);
                tripDefaultCostReq.setSellingPriceReq(sellingPriceReq);
                //团综合成本
                List<SeriesTripScheduleDefaultCostGroup> defaultCostGroupList = seriesTripScheduleDefaultCostGroupMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostGroup>().lambda().eq(SeriesTripScheduleDefaultCostGroup::getCostId, tripDefaultCostReq.getId()).eq(SeriesTripScheduleDefaultCostGroup::getIsDeleted, 0));
                if (CollectionUtils.isNotEmpty(defaultCostGroupList)) {
                    List<SeriesTripDefaultCostGroupReq> costGroupList = EntityUtil.copyList(defaultCostGroupList, SeriesTripDefaultCostGroupReq.class);
                    tripDefaultCostReq.setCostGroupList(costGroupList);
                }
                //调整费用
                List<SeriesTripScheduleDefaultFeeAdjust> defaultFeeAdjustList = seriesTripScheduleDefaultFeeAdjustMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultFeeAdjust>().lambda().eq(SeriesTripScheduleDefaultFeeAdjust::getCostId, tripDefaultCostReq.getId()).eq(SeriesTripScheduleDefaultFeeAdjust::getIsDeleted, 0));
                if (CollectionUtils.isNotEmpty(defaultFeeAdjustList)) {
                    List<SeriesTripDefaultFeeAdjustReq> feeAdjustList = EntityUtil.copyList(defaultFeeAdjustList, SeriesTripDefaultFeeAdjustReq.class);
                    tripDefaultCostReq.setFeeAdjustList(feeAdjustList);
                }
                //代收费用
                List<SeriesTripScheduleDefaultCostCollection> defaultCostCollectionList = seriesTripScheduleDefaultCostCollectionMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostCollection>().lambda().eq(SeriesTripScheduleDefaultCostCollection::getCostId, tripDefaultCostReq.getId()).eq(SeriesTripScheduleDefaultCostCollection::getIsDeleted, 0));
                if (CollectionUtils.isNotEmpty(defaultCostCollectionList)) {
                    List<SeriesTripDefaultCostCollectionReq> costCollectionList = new ArrayList<>();
                    for (SeriesTripScheduleDefaultCostCollection seriesTripScheduleDefaultCostCollection : defaultCostCollectionList) {
                        SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq = new SeriesTripDefaultCostCollectionReq();
                        BeanUtils.copyProperties(seriesTripScheduleDefaultCostCollection, seriesTripDefaultCostCollectionReq);
                        List<SeriesTripScheduleDefaultCostTouristType> defaultCostCollectionTouristTypeList = seriesTripScheduleDefaultCostTouristTypeMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostTouristType>().lambda().eq(SeriesTripScheduleDefaultCostTouristType::getCollectionId, seriesTripScheduleDefaultCostCollection.getId()).eq(SeriesTripScheduleDefaultCostTouristType::getIsDeleted, 0));
                        List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = EntityUtil.copyList(defaultCostCollectionTouristTypeList, SeriesTripDefaultCostCollectionTypeReq.class);
                        seriesTripDefaultCostCollectionReq.setCostCollectionTypeList(costCollectionTypeList);
                        costCollectionList.add(seriesTripDefaultCostCollectionReq);
                    }
                    tripDefaultCostReq.setCostCollectionList(costCollectionList);
                }
                tripDefaultCostReqList.add(tripDefaultCostReq);
            }
            tripScheduleCostDTO.setCostReqList(tripDefaultCostReqList);
        }

        return tripScheduleCostDTO;
    }

    @Override
    public TripScheduleDetailsDTO getTripScheduleDetailsId(Long id) {
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(id);
        Long tripId = seriesTripSchedule.getTripId();
        TripScheduleDetailsDTO tripScheduleDetailsDTO = new TripScheduleDetailsDTO();
        if (tripId != null) {
            SeriesTripScheduleItineraryDayReq seriesTripScheduleItineraryDayReq = seriesTripService.tripItineraryInformation(tripId);
            SeriesTripScheduleItineraryDayDTO seriesTripScheduleItineraryDayDTO = new SeriesTripScheduleItineraryDayDTO();
            BeanUtils.copyProperties(seriesTripScheduleItineraryDayReq, seriesTripScheduleItineraryDayDTO);
            tripScheduleDetailsDTO.setSeriesTripScheduleItineraryDayDTO(seriesTripScheduleItineraryDayDTO);

            TripSupplementReq tripSupplementReq = seriesTripService.tripSupplement(tripId);
            TripScheduleSupplementDTO tripScheduleSupplementDTO = new TripScheduleSupplementDTO();
            BeanUtils.copyProperties(tripSupplementReq, tripScheduleSupplementDTO);
            tripScheduleDetailsDTO.setScheduleSupplementDTO(tripScheduleSupplementDTO);

            List<TripFileDTO> tripFileList = seriesTripService.tripFileList(tripId);
            List<TripScheduleFileDTO> tripScheduleFileList = EntityUtil.copyList(tripFileList, TripScheduleFileDTO.class);
            tripScheduleDetailsDTO.setTripScheduleFileDTOList(tripScheduleFileList);
            // todo 收款明细,付款明细

        }
        return tripScheduleDetailsDTO;
    }

    @Override
    public void updateScheduleStatus(UpdateTripScheduleStatusReq updateTripScheduleStatusReq) {
        SeriesTripSchedule seriesTripSchedule = new SeriesTripSchedule();
        seriesTripSchedule.setId(updateTripScheduleStatusReq.getId());
        Integer type = updateTripScheduleStatusReq.getStatusType();
        seriesTripSchedule.setInternalSummary(updateTripScheduleStatusReq.getInternalSummary());
        seriesTripSchedule.setLastStatus(updateTripScheduleStatusReq.getScheduleStatus());
        //todo 调用供应商预订接口
        if (type == 1) {
            seriesTripSchedule.setScheduleStatus(4);
        }
        if (type == 2) {
            seriesTripSchedule.setScheduleStatus(5);
        }
        seriesTripScheduleMapper.updateById(seriesTripSchedule);

    }

    @Override
    public void updateTripStatusReq(UpdateTripStatusReq updateTripStatusReq) {
        SeriesTripSchedule seriesTripSchedule = new SeriesTripSchedule();
        seriesTripSchedule.setId(updateTripStatusReq.getId());
        Integer type = updateTripStatusReq.getStatusType();
        seriesTripSchedule.setLastStatus(updateTripStatusReq.getScheduleStatus());
        if (type == 1) {
            seriesTripSchedule.setScheduleStatus(3);
        }
        if (type == 2) {
            seriesTripSchedule.setScheduleStatus(6);
        }
        if (type == 3) {
            seriesTripSchedule.setScheduleStatus(2);
        }
        if (type == 4) {
            seriesTripSchedule.setScheduleStatus(6);
        }
        if (type == 5) {
            SeriesTripSchedule tripSchedule = seriesTripScheduleMapper.selectById(updateTripStatusReq.getId());
            seriesTripSchedule.setScheduleStatus(tripSchedule.getLastStatus());
        }
        //开售
        if (type == 6) {
            SeriesTripSchedule tripSchedule = seriesTripScheduleMapper.assertExistById(updateTripStatusReq.getId());
            seriesTripSchedule.setScheduleStatus(tripSchedule.getLastStatus());
        }
        seriesTripScheduleMapper.updateById(seriesTripSchedule);
    }

    @Override
    public TripScheduleGroupDTO group(Long id) {
        TripScheduleGroupDTO tripScheduleGroupDTO = new TripScheduleGroupDTO();
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(id);
        SeriesTripScheduleTeamRule seriesTripScheduleTeamRule = seriesTripScheduleTeamRuleMapper.selectOne(new QueryWrapper<SeriesTripScheduleTeamRule>().lambda().eq(SeriesTripScheduleTeamRule::getScheduleId, id));
        Integer lockedTouristNum = seriesTripSchedule.getLockedTouristNum();
        Integer minPeople = seriesTripScheduleTeamRule.getMinPeople();
        if (lockedTouristNum > minPeople) {
            tripScheduleGroupDTO.setType(1);
        } else {
            tripScheduleGroupDTO.setType(2);
        }
        return tripScheduleGroupDTO;
    }

    /**
     * 通知派团
     */
    @Override
    public void notice(TripScheduleNoticeReq req, TokenUser tokenUser) {
        //查询团期
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(req.getId());
        if (null == seriesTripSchedule) {
            throw new BusinessException(SystemError.SYS_411);
        }

        GroupDistributionAddReq callReq = new GroupDistributionAddReq();
        callReq.setGroupType(1);
        callReq.setGroupStatus(seriesTripSchedule.getScheduleStatus());
        callReq.setGroupId(seriesTripSchedule.getId());
        callReq.setGroupNumber(seriesTripSchedule.getTripNumber());
        callReq.setDays(seriesTripSchedule.getTravelDays());
        callReq.setType(req.getType());
        groupDistributionService.add(callReq, tokenUser);
    }

    /**
     * 茶会通知
     */
    @Transactional
    @Override
    public void teaParty(TripScheduleTeaPartyReq req, TokenUser tokenUser) {
        //查询团期
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(req.getId());
        if (null == seriesTripSchedule) {
            throw new BusinessException(SystemError.SYS_411);
        }

        //创建茶会
        TeaPartyAddReq teaPartyAddReq = new TeaPartyAddReq();
        teaPartyAddReq.setGroupType(1);
        teaPartyAddReq.setGroupStatus(seriesTripSchedule.getScheduleStatus());
        teaPartyAddReq.setGroupId(seriesTripSchedule.getId());
        teaPartyAddReq.setGroupNumber(seriesTripSchedule.getTripNumber());
        teaPartyAddReq.setDays(seriesTripSchedule.getTravelDays());
        teaPartyAddReq.setReceivedNumber(seriesTripSchedule.getLockedTouristNum());
        teaPartyAddReq.setTeaPartyTime(DateUtil.toLocalDateTime(req.getTeaPartyDate(), req.getTeaPartyTime()));
        teaPartyAddReq.setDepartureDate(DateUtil.toLocalDateTime(seriesTripSchedule.getDepartureDate(), seriesTripSchedule.getDepartureTime()));
        teaPartyAddReq.setTeaPartyAreaId(req.getTeaPartyLocId());
        teaPartyAddReq.setTeaPartyAreaName(req.getTeaPartyLocName());
        teaPartyAddReq.setTeaPartyRoom(req.getTeaPartyRoom());
        Result add = teaPartyService.add(teaPartyAddReq, tokenUser);
        if (!add.isSuccess()) {
            throw new BusinessException(SystemError.SYS_439, "导领中心", add.getMsg());
        }
        SeriesTripScheduleTeamRule seriesTripScheduleTeamRule = new SeriesTripScheduleTeamRule();
        seriesTripScheduleTeamRule.setTeaPartyLocId(req.getTeaPartyLocId());
        seriesTripScheduleTeamRule.setTeaPartyDate(req.getTeaPartyDate());
        seriesTripScheduleTeamRule.setTeaPartyTime(req.getTeaPartyTime());
        seriesTripScheduleTeamRule.setScheduleId(req.getId());
        seriesTripScheduleTeamRule.setTeaPartyRemark(req.getTeaPartyRemark());
        seriesTripScheduleTeamRuleMapper.update(seriesTripScheduleTeamRule, new QueryWrapper<SeriesTripScheduleTeamRule>().lambda().eq(SeriesTripScheduleTeamRule::getScheduleId, req.getId()));
    }

    @Override
    public List<ScheduleCalendarDTO> scheduleDepartureList(ScheduleDepartureListQueryReq scheduleDepartureListQueryReq) {


        List<LocalDate> dateList = getDayListOfMonth(scheduleDepartureListQueryReq.getYear(), scheduleDepartureListQueryReq.getMonth());

        HashMap<LocalDate, List<TripScheduleDepartureDTO>> localDateMap = new HashMap<>(dateList.size());
        if (CollectionUtils.isNotEmpty(dateList)) {
            for (LocalDate localDate : dateList) {
                localDateMap.put(localDate, new ArrayList<>());
            }
        }

        List<TripScheduleDepartureDTO> departureDTOList = seriesTripScheduleMapper.scheduleDepartureList(scheduleDepartureListQueryReq);

        for (TripScheduleDepartureDTO tripScheduleDepartureDTO : departureDTOList) {

            Integer costSharingType = tripScheduleDepartureDTO.getCostSharingType();
            LocalDate localDate = tripScheduleDepartureDTO.getDepartureDate();
            if (null != localDateMap.get(localDate)) {
                List<TripScheduleDepartureDTO> departureDTOS = localDateMap.get(localDate);
                departureDTOS.add(tripScheduleDepartureDTO);
            }
            if (localDate != null) {
                DayOfWeek dayOfWeek = localDate.getDayOfWeek();
                tripScheduleDepartureDTO.setDayOfWeek(dayOfWeek);
            }
            Long id = tripScheduleDepartureDTO.getId();
            if (costSharingType == 1) {
                //成本分摊主表
                SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCostDefaultCost = seriesTripScheduleDefaultCostMapper.selectOne(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, id).eq(SeriesTripScheduleDefaultCost::getIsDeleted, IsDeletedCode.NO.getCode()));
                TripScheduleDefaultCostDTO tripScheduleDefaultCostDTO = new TripScheduleDefaultCostDTO();
                tripScheduleDefaultCostDTO.setId(seriesTripScheduleDefaultCostDefaultCost.getId());
                tripScheduleDefaultCostDTO.setTripId(seriesTripScheduleDefaultCostDefaultCost.getTripId());
                SeriesTripScheduleSellingPriceDTO seriesTripScheduleSellingPriceDTO = new SeriesTripScheduleSellingPriceDTO();
                BeanUtils.copyProperties(seriesTripScheduleDefaultCostDefaultCost, seriesTripScheduleSellingPriceDTO);
                tripScheduleDefaultCostDTO.setSeriesTripScheduleSellingPriceDTO(seriesTripScheduleSellingPriceDTO);
                //团综合成本
                List<SeriesTripScheduleDefaultCostGroup> seriesTripScheduleDefaultCostGroupList = seriesTripScheduleDefaultCostGroupMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostGroup>().lambda().eq(SeriesTripScheduleDefaultCostGroup::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()));
                if (CollectionUtils.isNotEmpty(seriesTripScheduleDefaultCostGroupList)) {
                    List<SeriesTripScheduleDefaultCostGroupDTO> seriesTripDefaultCostGroupReqList = EntityUtil.copyList(seriesTripScheduleDefaultCostGroupList, SeriesTripScheduleDefaultCostGroupDTO.class);
                    tripScheduleDefaultCostDTO.setCostGroupList(seriesTripDefaultCostGroupReqList);
                }
                //调整费用
                List<SeriesTripScheduleDefaultFeeAdjust> defaultFeeAdjustList = seriesTripScheduleDefaultFeeAdjustMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultFeeAdjust>().lambda().eq(SeriesTripScheduleDefaultFeeAdjust::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()));
                if (CollectionUtils.isNotEmpty(defaultFeeAdjustList)) {
                    List<SeriesTripScheduleDefaultFeeAdjustDTO> feeAdjustList = EntityUtil.copyList(defaultFeeAdjustList, SeriesTripScheduleDefaultFeeAdjustDTO.class);
                    tripScheduleDefaultCostDTO.setFeeAdjustList(feeAdjustList);
                }
                //代收费用
                List<SeriesTripScheduleDefaultCostCollection> defaultCostCollectionList = seriesTripScheduleDefaultCostCollectionMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostCollection>().lambda().eq(SeriesTripScheduleDefaultCostCollection::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()));
                if (CollectionUtils.isNotEmpty(defaultCostCollectionList)) {
                    List<SeriesTripScheduleDefaultCostCollectionDTO> costCollectionList = new ArrayList<>();
                    for (SeriesTripScheduleDefaultCostCollection seriesTripScheduleDefaultCostCollection : defaultCostCollectionList) {
                        SeriesTripScheduleDefaultCostCollectionDTO seriesTripScheduleDefaultCostCollectionDTO = new SeriesTripScheduleDefaultCostCollectionDTO();
                        BeanUtils.copyProperties(seriesTripScheduleDefaultCostCollection, seriesTripScheduleDefaultCostCollectionDTO);
                        List<SeriesTripScheduleDefaultCostTouristType> defaultCostCollectionTouristTypeList = seriesTripScheduleDefaultCostTouristTypeMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostTouristType>().lambda().eq(SeriesTripScheduleDefaultCostTouristType::getCollectionId, seriesTripScheduleDefaultCostCollection.getId()));
                        List<SeriesTripScheduleDefaultCostTouristTypeDTO> costCollectionTypeList = EntityUtil.copyList(defaultCostCollectionTouristTypeList, SeriesTripScheduleDefaultCostTouristTypeDTO.class);
                        seriesTripScheduleDefaultCostCollectionDTO.setCostCollectionTypeList(costCollectionTypeList);
                        costCollectionList.add(seriesTripScheduleDefaultCostCollectionDTO);
                    }
                    tripScheduleDefaultCostDTO.setCostCollectionList(costCollectionList);
                }

                tripScheduleDepartureDTO.setTripScheduleDefaultCost(tripScheduleDefaultCostDTO);
            }

            if (costSharingType == 2) {
                //成本分摊主表
                TripCustomTreeReq tripCustomTree = new TripCustomTreeReq();
                List<SeriesTripScheduleTouristType> seriesTripTouristTypeList = seriesTripScheduleTouristTypeService.list(new QueryWrapper<SeriesTripScheduleTouristType>().lambda().eq(SeriesTripScheduleTouristType::getScheduleId, id).isNotNull(SeriesTripScheduleTouristType::getAgeBegin).orderByDesc(SeriesTripScheduleTouristType::getSort));
                List<TripCustomTreePassengerReq> touristTypeList = EntityUtil.copyList(seriesTripTouristTypeList, TripCustomTreePassengerReq.class);
                tripCustomTree.setPassengerList(touristTypeList);
                List<SeriesTripScheduleCustomDateSubsection> seriesTripCustomDateSubsectionList = customDateSubsectionService.list(new QueryWrapper<SeriesTripScheduleCustomDateSubsection>().lambda().eq(SeriesTripScheduleCustomDateSubsection::getScheduleId, id));
                List<TripCustomTreeDateReq> treeDateList = EntityUtil.copyList(seriesTripCustomDateSubsectionList, TripCustomTreeDateReq.class);
                tripCustomTree.setDateList(treeDateList);
                tripScheduleDepartureDTO.setTripCustomTree(tripCustomTree);

                SeriesTripScheduleCustomCost seriesTripCustomCost = customCostService.getOne(new QueryWrapper<SeriesTripScheduleCustomCost>().lambda().eq(SeriesTripScheduleCustomCost::getScheduleId, id));
                TripCustomCostReq tripCustomCostReq = null;
                tripCustomCostReq = new TripCustomCostReq();
                tripCustomCostReq.setId(seriesTripCustomCost.getId());
                tripCustomCostReq.setName(seriesTripCustomCost.getName());
                tripCustomCostReq.setTripId(seriesTripCustomCost.getTripId());
                List<SeriesTripScheduleCustomSalePrice> seriesTripCustomSalePriceList = customSalePriceService.list(new QueryWrapper<SeriesTripScheduleCustomSalePrice>().lambda().eq(SeriesTripScheduleCustomSalePrice::getCostId, seriesTripCustomCost.getId()));
                List<TripCustomSalePriceReq> customSalePriceList = new ArrayList<>();
                for (SeriesTripScheduleCustomSalePrice seriesTripCustomSalePrice : seriesTripCustomSalePriceList) {
                    TripCustomSalePriceReq tripCustomSalePriceReq = new TripCustomSalePriceReq();
                    BeanUtils.copyProperties(seriesTripCustomSalePrice, tripCustomSalePriceReq);
                    SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomSalePrice.getDateSubsectionId());
                    if (seriesTripCustomDateSubsection != null) {
                        tripCustomSalePriceReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                        tripCustomSalePriceReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                        tripCustomSalePriceReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                        tripCustomSalePriceReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                        tripCustomSalePriceReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                        tripCustomSalePriceReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                        tripCustomSalePriceReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                        tripCustomSalePriceReq.setDateId(seriesTripCustomDateSubsection.getId());
                    }
                    SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomSalePrice.getTripTouristTypeId());
                    if (seriesTripTouristType != null) {
                        tripCustomSalePriceReq.setPassengerTypeId(seriesTripTouristType.getId());
                        tripCustomSalePriceReq.setMainDataId(seriesTripTouristType.getMainDataId());
                        tripCustomSalePriceReq.setName(seriesTripTouristType.getName());
                        tripCustomSalePriceReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                        tripCustomSalePriceReq.setAgeEnd(seriesTripTouristType.getAgeEnd());
                    }
                    customSalePriceList.add(tripCustomSalePriceReq);
                }
                tripCustomCostReq.setCustomSalePriceList(customSalePriceList);

                //团综合成本
                List<SeriesTripScheduleCustomCostGroup> customCostGroupList = customCostGroupService.list(new QueryWrapper<SeriesTripScheduleCustomCostGroup>().lambda().eq(SeriesTripScheduleCustomCostGroup::getCostId, seriesTripCustomCost.getId()));
                if (CollectionUtils.isNotEmpty(customCostGroupList)) {
                    List<SeriesTripCustomCostGroupReq> costGroupList = EntityUtil.copyList(customCostGroupList, SeriesTripCustomCostGroupReq.class);

                    for (SeriesTripCustomCostGroupReq seriesTripCustomCostGroupReq : costGroupList) {
                        List<CustomCostReq> customCostReqs = new ArrayList<>();
                        List<SeriesTripScheduleCustomCostGroupAge> customCostGroupAgeList = customCostGroupAgeService.list(new QueryWrapper<SeriesTripScheduleCustomCostGroupAge>().lambda().eq(SeriesTripScheduleCustomCostGroupAge::getGroupCostId, seriesTripCustomCostGroupReq.getId()));
                        for (SeriesTripScheduleCustomCostGroupAge seriesTripCustomCostGroupAge : customCostGroupAgeList) {
                            CustomCostReq customCostReq = new CustomCostReq();
                            customCostReq.setPrice(seriesTripCustomCostGroupAge.getPrice());
                            SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomCostGroupAge.getDateSubsectionId());
                            if (seriesTripCustomDateSubsection != null) {
                                customCostReq.setDateId(seriesTripCustomDateSubsection.getId());
                                customCostReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                                customCostReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                                customCostReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                                customCostReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                                customCostReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                                customCostReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                            }
                            SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomCostGroupAge.getTripTouristTypeId());
                            if (seriesTripTouristType != null) {
                                customCostReq.setMainDataId(seriesTripTouristType.getMainDataId());
                                customCostReq.setName(seriesTripTouristType.getName());
                                customCostReq.setAgeBegin(seriesTripTouristType.getAgeEnd());
                                customCostReq.setAgeEnd(seriesTripTouristType.getAgeEnd());
                                customCostReq.setPassengerTypeId(seriesTripTouristType.getId());
                            }
                            customCostReqs.add(customCostReq);
                        }
                        seriesTripCustomCostGroupReq.setCustomCostList(customCostReqs);
                    }

                    tripCustomCostReq.setCostGroupList(costGroupList);
                }
                //调整费用
                List<SeriesTripScheduleCustomFeeAdjust> customFeeAdjustList = customFeeAdjustService.list(new QueryWrapper<SeriesTripScheduleCustomFeeAdjust>().lambda().eq(SeriesTripScheduleCustomFeeAdjust::getCostId, seriesTripCustomCost.getId()));
                if (CollectionUtils.isNotEmpty(customFeeAdjustList)) {
                    List<SeriesTripCustomFeeAdjustReq> feeAdjustList = EntityUtil.copyList(customFeeAdjustList, SeriesTripCustomFeeAdjustReq.class);

                    for (SeriesTripCustomFeeAdjustReq seriesTripCustomFeeAdjustReq : feeAdjustList) {
                        List<CustomCostReq> customCostReqs = new ArrayList<>();
                        List<SeriesTripScheduleCustomAdjustTourist> customFeeAdjustTouristTypeList = customAdjustTouristService.list(new QueryWrapper<SeriesTripScheduleCustomAdjustTourist>().lambda().eq(SeriesTripScheduleCustomAdjustTourist::getAdjustId, seriesTripCustomFeeAdjustReq.getId()));
                        for (SeriesTripScheduleCustomAdjustTourist seriesTripCustomFeeAdjustTouristType : customFeeAdjustTouristTypeList) {
                            CustomCostReq customCostReq = new CustomCostReq();
                            customCostReq.setPrice(seriesTripCustomFeeAdjustTouristType.getPrice());
                            SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomFeeAdjustTouristType.getDateSubsectionId());
                            if (seriesTripCustomDateSubsection != null) {
                                customCostReq.setDateId(seriesTripCustomDateSubsection.getId());
                                customCostReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                                customCostReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                                customCostReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                                customCostReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                                customCostReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                                customCostReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                            }

                            SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomFeeAdjustTouristType.getTripTouristTypeId());
                            if (seriesTripTouristType != null) {
                                customCostReq.setPassengerTypeId(seriesTripTouristType.getId());
                                customCostReq.setMainDataId(seriesTripTouristType.getMainDataId());
                                customCostReq.setName(seriesTripTouristType.getName());
                                customCostReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                                customCostReq.setAgeEnd(seriesTripTouristType.getAgeEnd());

                            }
                            customCostReqs.add(customCostReq);
                        }
                        seriesTripCustomFeeAdjustReq.setCustomCostList(customCostReqs);
                    }

                    tripCustomCostReq.setFeeAdjustList(feeAdjustList);
                }
                //代收费用
                List<SeriesTripScheduleCustomCostCollection> customCostCollectionList = customCostCollectionService.list(new QueryWrapper<SeriesTripScheduleCustomCostCollection>().lambda().eq(SeriesTripScheduleCustomCostCollection::getCostId, seriesTripCustomCost.getId()));
                if (CollectionUtils.isNotEmpty(customCostCollectionList)) {
                    List<SeriesTripDefaultCostCollectionReq> costCollectionList = new ArrayList<>();
                    for (SeriesTripScheduleCustomCostCollection seriesTripCustomCostCollection : customCostCollectionList) {
                        SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq = new SeriesTripDefaultCostCollectionReq();
                        BeanUtils.copyProperties(seriesTripCustomCostCollection, seriesTripDefaultCostCollectionReq);
                        List<SeriesTripScheduleCustomTouristType> customCostCollectionTouristTypeList = customTouristTypeService.list(new QueryWrapper<SeriesTripScheduleCustomTouristType>().lambda().eq(SeriesTripScheduleCustomTouristType::getCollectionId, seriesTripCustomCostCollection.getId()));
                        List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = EntityUtil.copyList(customCostCollectionTouristTypeList, SeriesTripDefaultCostCollectionTypeReq.class);
                        seriesTripDefaultCostCollectionReq.setCostCollectionTypeList(costCollectionTypeList);
                        costCollectionList.add(seriesTripDefaultCostCollectionReq);
                    }
                    tripCustomCostReq.setCostCollectionList(costCollectionList);
                }

                tripScheduleDepartureDTO.setTripCustomCostReq(tripCustomCostReq);
            }

        }
        List<ScheduleCalendarDTO> scheduleCalendarDTOList = new ArrayList<>(dateList.size());
        for (Map.Entry<LocalDate, List<TripScheduleDepartureDTO>> entry : localDateMap.entrySet()) {
            ScheduleCalendarDTO scheduleCalendarDTO = new ScheduleCalendarDTO();
            scheduleCalendarDTO.setLocalDate(entry.getKey());
            scheduleCalendarDTO.setTripScheduleDepartureDTOList(entry.getValue());
            scheduleCalendarDTOList.add(scheduleCalendarDTO);
        }


        return scheduleCalendarDTOList;
    }

    @Override
    @Transactional
    public Result updateScheduleNum(UpdateScheduleNumReq updateScheduleNumReq) {
        //查收已收客人数
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(updateScheduleNumReq.getId());
        Integer lockedTouristNum = seriesTripSchedule.getLockedTouristNum();
        SeriesTripSchedule tripSchedule = new SeriesTripSchedule();
        Integer num = lockedTouristNum - updateScheduleNumReq.getNum();
        if (num >= 0) {
            tripSchedule.setId(updateScheduleNumReq.getId());
            tripSchedule.setLockedTouristNum(num);
            seriesTripScheduleMapper.updateById(tripSchedule);
            return Result.success();
        } else {
            return Result.failed(SystemError.TOUR_ORDER_ERROR);
        }
    }

    @Override
    public List<TripScheduleAdjustmentDTO> adjustmentItems(Long id) {

        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(id);
        List<TripScheduleAdjustmentDTO> scheduleAdjustmentDTOS = new ArrayList<>();
        if (seriesTripSchedule.getCostSharingType() == 1) {
            //成本分摊主表
            SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCostDefaultCost = seriesTripScheduleDefaultCostMapper.selectOne(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, id).eq(SeriesTripScheduleDefaultCost::getIsDeleted, IsDeletedCode.NO.getCode()));
            //调整费用
            List<SeriesTripScheduleDefaultFeeAdjust> defaultFeeAdjustList = seriesTripScheduleDefaultFeeAdjustMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultFeeAdjust>().lambda().eq(SeriesTripScheduleDefaultFeeAdjust::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()));
            if (CollectionUtils.isNotEmpty(defaultFeeAdjustList)) {
                scheduleAdjustmentDTOS = EntityUtil.copyList(defaultFeeAdjustList, TripScheduleAdjustmentDTO.class);
            }
        }
        if (seriesTripSchedule.getCostSharingType() == 2) {
            //成本分摊主表
            SeriesTripScheduleCustomCost seriesTripCustomCost = customCostService.getOne(new QueryWrapper<SeriesTripScheduleCustomCost>().lambda().eq(SeriesTripScheduleCustomCost::getScheduleId, id));

            //调整费用
            List<SeriesTripScheduleCustomFeeAdjust> customFeeAdjustList = customFeeAdjustService.list(new QueryWrapper<SeriesTripScheduleCustomFeeAdjust>().lambda().eq(SeriesTripScheduleCustomFeeAdjust::getCostId, seriesTripCustomCost.getId()));
            if (CollectionUtils.isNotEmpty(customFeeAdjustList)) {
                for (SeriesTripScheduleCustomFeeAdjust seriesTripScheduleCustomFeeAdjust : customFeeAdjustList) {


                    List<SeriesTripScheduleCustomAdjustTourist> customFeeAdjustTouristTypeList = customAdjustTouristService.list(new QueryWrapper<SeriesTripScheduleCustomAdjustTourist>().lambda().eq(SeriesTripScheduleCustomAdjustTourist::getAdjustId, seriesTripScheduleCustomFeeAdjust.getId()));
                    for (SeriesTripScheduleCustomAdjustTourist seriesTripCustomFeeAdjustTouristType : customFeeAdjustTouristTypeList) {
                        boolean flag = true;
                        TripScheduleAdjustmentDTO scheduleAdjustmentDTO = new TripScheduleAdjustmentDTO();
                        scheduleAdjustmentDTO.setCostAmount(seriesTripCustomFeeAdjustTouristType.getPrice());
                        BeanUtils.copyProperties(seriesTripScheduleCustomFeeAdjust, scheduleAdjustmentDTO);
                        SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomFeeAdjustTouristType.getDateSubsectionId());
                        if (seriesTripCustomDateSubsection != null) {
                            Integer dateType = seriesTripCustomDateSubsection.getDateType();
                            LocalDate departureDate = seriesTripSchedule.getDepartureDate();
                            if (dateType == 1) {

                                Integer dayOfWeek = departureDate.getDayOfWeek().getValue();
                                List<Integer> weekList = seriesTripCustomDateSubsection.getCheckWeekDays();
                                if (!weekList.contains(dayOfWeek)) {
                                    flag = false;
                                }
                            }
                            if (dateType == 2) {
                                LocalDate dateBegin = seriesTripCustomDateSubsection.getDateBegin();
                                LocalDate dateEnd = seriesTripCustomDateSubsection.getDateEnd();
                                if (departureDate.isAfter(dateBegin) || departureDate.isBefore(dateEnd)) {
                                    flag = false;
                                }

                            }

                        }

                        SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomFeeAdjustTouristType.getTripTouristTypeId());
                        if (seriesTripTouristType != null) {
                            scheduleAdjustmentDTO.setTripTouristTypeId(seriesTripTouristType.getMainDataId());
                            scheduleAdjustmentDTO.setTripTouristTypeName(seriesTripTouristType.getName());
                        }
                        if (flag) {
                            scheduleAdjustmentDTOS.add(scheduleAdjustmentDTO);
                        }
                    }

                }
            }


        }


        return scheduleAdjustmentDTOS;
    }

    @Override
    public Result<PageResponse<TripScheduleDTO>> scheduleBookList(TripScheduleListQueryReq tripScheduleListQueryReq, TokenUser tokenUser) {
        //只查询未成團、已成團
        tripScheduleListQueryReq.setScheduleStatusList(Assist.asList(1, 2));
        //只查当前日期往后的
        tripScheduleListQueryReq.setDepartureBeginDate(LocalDate.now());
        //负责部门
        if (tripScheduleListQueryReq.getChargePersonDeptId() != null) {
            List<Long> chargePersonDeptIdList = sysDepartmentService.queryUserIdsByDeptId(tripScheduleListQueryReq.getChargePersonDeptId());
            if (Assist.isEmpty(chargePersonDeptIdList)) {
                return newPageResult();
            }
            tripScheduleListQueryReq.setChargePersonIdList(chargePersonDeptIdList);
        }

        //自营
        if (tripScheduleListQueryReq.getType() == 1) {
            List<Long> permissionList = sysDepartmentService.queryIdsByUserId(tokenUser.getId());
            Assist.add(tripScheduleListQueryReq.getOfflineSelfCustomPermissionIdList(), permissionList);
            Assist.add(tripScheduleListQueryReq.getOfflineSelfCustomPermissionIdList(), tokenUser.getId());
            if (CollectionUtils.isEmpty(tripScheduleListQueryReq.getOfflineSelfCustomPermissionIdList())) {
                return newPageResult();
            }
        } else if (tripScheduleListQueryReq.getType() == 2) {  //代理商
            tripScheduleListQueryReq.setQueryAgent(true);
        } else {
            throw new BusinessException(SystemError.TOUR_ORDER_30009);
        }

        Set<Long> scheduleIdSet = new HashSet<>();
        List<Integer> typeList = tripScheduleListQueryReq.getTypeList();
        if (CollectionUtils.isNotEmpty(typeList)) {
            List<Long> itemScheduleIdList = new ArrayList<>();
            //仅看我收客的团
            if (tripScheduleListQueryReq.getTypeList().contains(5)) {
                SeriesOrderMyGroupIdQueryReq seriesOrderMyGroupIdQueryReq = new SeriesOrderMyGroupIdQueryReq();
                seriesOrderMyGroupIdQueryReq.setUserId(tokenUser.getId());
                seriesOrderMyGroupIdQueryReq.setDepartDateBegin(LocalDate.now());
                itemScheduleIdList = seriesOrderService.queryMyGroupId(seriesOrderMyGroupIdQueryReq);
                if (Assist.isEmpty(itemScheduleIdList)) {
                    return newPageResult();
                }
                Assist.add(scheduleIdSet, itemScheduleIdList);
            }
            //可用银行优惠
            if (tripScheduleListQueryReq.getTypeList().contains(6)) {
                itemScheduleIdList = operateBankPreferenceService.queryRelationIdList(1);
                if (Assist.isEmpty(itemScheduleIdList)) {
                    return newPageResult();
                }
                Assist.add(scheduleIdSet, itemScheduleIdList);
            }
            //可用优惠
            if (tripScheduleListQueryReq.getTypeList().contains(7)) {
                itemScheduleIdList = operatePromotionService.queryRelationIdList(1);
                if (Assist.isEmpty(itemScheduleIdList)) {
                    return newPageResult();
                }
                Assist.add(scheduleIdSet, itemScheduleIdList);
            }
        }

        //团期ID
        tripScheduleListQueryReq.setScheduleIdList(Assist.toList(scheduleIdSet));

        IPage<SeriesTripSchedule> page = new Page<>(tripScheduleListQueryReq.getPageNo(), tripScheduleListQueryReq.getPageSize());
        IPage<SeriesTripSchedule> iPage = seriesTripScheduleMapper.scheduleList(page, tripScheduleListQueryReq, true);
        List<SeriesTripSchedule> seriesTripScheduleList = iPage.getRecords();
        List<TripScheduleDTO> scheduleDTOList = EntityUtil.copyList(seriesTripScheduleList, TripScheduleDTO.class);

        for (TripScheduleDTO tripScheduleDTO : scheduleDTOList) {

            JSONArray jsonArray = JSONArray.parseArray(tripScheduleDTO.getCollectionPlaceJsons());
            List<CollectionPlaceJson> collectionPlaceJson = JSONObject.parseArray(jsonArray.toJSONString(), CollectionPlaceJson.class);
            tripScheduleDTO.setCollectionPlaceJson(collectionPlaceJson);
            JSONArray jsonArrayDissolution = JSONArray.parseArray(tripScheduleDTO.getCollectionPlaceJsons());
            List<CollectionPlaceJson> dissolutionAreaJson = JSONObject.parseArray(jsonArrayDissolution.toJSONString(), CollectionPlaceJson.class);
            tripScheduleDTO.setDissolutionAreaJson(dissolutionAreaJson);

            Integer costSharingType = tripScheduleDTO.getCostSharingType();
            LocalDate departureDate = tripScheduleDTO.getDepartureDate();
            if (departureDate != null) {
                DayOfWeek dayOfWeek = departureDate.getDayOfWeek();
                tripScheduleDTO.setDepartureDayOfWeek(dayOfWeek);
            }
            LocalDate backDate = tripScheduleDTO.getBackDate();
            if (backDate != null) {
                DayOfWeek dayOfWeek = departureDate.getDayOfWeek();
                tripScheduleDTO.setBackDayOfWeek(dayOfWeek);
            }
//            UserDTO userDto = (UserDTO) redisTemplate.opsForValue().get(RedisConstants.ACCOUNT_QUERY_KEY + tripScheduleDTO.getChargePersonId());
//            if (userDto!=null&& StringUtils.isNotBlank(userDto.getDept())){
//                tripScheduleDTO.setChargePersonDepartment(userDto.getDept());
//            }
//            UserDTO opDto = (UserDTO) redisTemplate.opsForValue().get(RedisConstants.ACCOUNT_QUERY_KEY + tripScheduleDTO.getOpId());
//            if (opDto!=null&&StringUtils.isNotBlank(opDto.getDept())){
//                tripScheduleDTO.setOpDepartment(opDto.getDept());
//            }

            BigDecimal incrementalPrice = new BigDecimal("0.00");
            Integer incrementalPriceType = 2;


//            //自营
//            if (tripScheduleListQueryReq.getType() == 1) {
//
//                if (tripScheduleDTO.getSelfSalePermissionType() == 2) {
//                    QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom> queryWrapper = new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>();
//                    queryWrapper.lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, tripScheduleDTO.getId())
//                            .eq(SeriesTripScheduleChannelOfflineSelfCustom::getIsDeleted, 0).isNull(SeriesTripScheduleChannelOfflineSelfCustom::getCheckType)
//                            .isNull(SeriesTripScheduleChannelOfflineSelfCustom::getRelationId);
//                    SeriesTripScheduleChannelOfflineSelfCustom seriesTripScheduleChannelOfflineSelfCustom = selfCustomMapper.selectOne(queryWrapper);
//                    if (null != seriesTripScheduleChannelOfflineSelfCustom) {
//                        incrementalPriceType = seriesTripScheduleChannelOfflineSelfCustom.getAdjustType();
//                        incrementalPrice = incrementalPrice.add(seriesTripScheduleChannelOfflineSelfCustom.getAmount());
//                    }
//                }
//                if (tripScheduleDTO.getSelfSalePermissionType() == 3) {
//                    QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom> queryWrapper = new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>();
//                    queryWrapper.lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, tripScheduleDTO.getId())
//                            .eq(SeriesTripScheduleChannelOfflineSelfCustom::getIsDeleted, 0).eq(SeriesTripScheduleChannelOfflineSelfCustom::getCheckType, 2)
//                            .eq(SeriesTripScheduleChannelOfflineSelfCustom::getRelationId, tokenUser.getId()).isNotNull(SeriesTripScheduleChannelOfflineSelfCustom::getAdjustType);
//                    SeriesTripScheduleChannelOfflineSelfCustom seriesTripScheduleChannelOfflineSelfCustom = selfCustomMapper.selectOne(queryWrapper);
//
//                    if (null != seriesTripScheduleChannelOfflineSelfCustom) {
//                        incrementalPriceType = seriesTripScheduleChannelOfflineSelfCustom.getAdjustType();
//                        incrementalPrice = incrementalPrice.add(seriesTripScheduleChannelOfflineSelfCustom.getAmount());
//                    } else {
//                        SysDepartmentDTO sysDepartmentDTO = sysDepartmentService.queryByUserId(tokenUser.getId());
//                        QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom> queryDepartmentWrapper = new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>();
//                        queryDepartmentWrapper.lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, tripScheduleDTO.getId())
//                                .eq(SeriesTripScheduleChannelOfflineSelfCustom::getIsDeleted, 0).eq(SeriesTripScheduleChannelOfflineSelfCustom::getCheckType, 1)
//                                .eq(SeriesTripScheduleChannelOfflineSelfCustom::getRelationId, sysDepartmentDTO.getId()).isNotNull(SeriesTripScheduleChannelOfflineSelfCustom::getAdjustType);
//                        SeriesTripScheduleChannelOfflineSelfCustom departmentSelfCustom = selfCustomMapper.selectOne(queryDepartmentWrapper);
//                        if (null != departmentSelfCustom) {
//                            incrementalPriceType = departmentSelfCustom.getAdjustType();
//                            incrementalPrice = incrementalPrice.add(departmentSelfCustom.getAmount());
//                        }
//
//                    }
//                }
//            }

            if (costSharingType == 1) {
                List<SeriesTripScheduleDefaultCost> defaultCostList = seriesTripScheduleDefaultCostMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, tripScheduleDTO.getId()));
                if (CollectionUtils.isNotEmpty(defaultCostList)) {
                    SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCost = defaultCostList.get(0);
                    if (seriesTripScheduleDefaultCost != null) {
                        BigDecimal customerPrice = calcSalePriceInfoOfflineAgent(tripScheduleDTO.getId(), tokenUser.getId(), seriesTripScheduleDefaultCost.getGroupCustomerPrice());
                        BigDecimal groupAgencyPrice = calcSalePriceInfoOfflineAgent(tripScheduleDTO.getId(), tokenUser.getId(), seriesTripScheduleDefaultCost.getGroupAgencyPrice());
//                        BigDecimal customerPrice = new BigDecimal("0.00");
//                        //类型
//                        if (incrementalPriceType == 1) {
//                            customerPrice = seriesTripScheduleDefaultCost.getGroupCustomerPrice().add(incrementalPrice);
//                        }
//                        if (incrementalPriceType == 0) {
//                            customerPrice = seriesTripScheduleDefaultCost.getGroupCustomerPrice().subtract(incrementalPrice);
//                        }
//                        if (incrementalPriceType == 2) {
//                            customerPrice = seriesTripScheduleDefaultCost.getGroupCustomerPrice();
//                        }
                        tripScheduleDTO.setGroupCustomerPrice(customerPrice);
                        tripScheduleDTO.setGroupAgencyPrice(groupAgencyPrice);
//
//
//                        tripScheduleDTO.setCustomerGrossProfitRate(seriesTripScheduleDefaultCost.getCustomerGrossProfitRate());
//                        tripScheduleDTO.setGroupAgencyPrice(seriesTripScheduleDefaultCost.getGroupAgencyPrice());
//                        tripScheduleDTO.setAgencyGrossProfitRate(seriesTripScheduleDefaultCost.getAgencyGrossProfitRate());
                    }
                }
            }
            if (costSharingType == 2) {
                List<SeriesTripScheduleCustomSalePrice> seriesTripScheduleCustomSalePrices = customSalePriceService.list(new QueryWrapper<SeriesTripScheduleCustomSalePrice>().lambda().eq(SeriesTripScheduleCustomSalePrice::getScheduleId, tripScheduleDTO.getId()));
                if (CollectionUtils.isNotEmpty(seriesTripScheduleCustomSalePrices)) {
                    SeriesTripScheduleCustomSalePrice seriesTripScheduleCustomSalePrice = seriesTripScheduleCustomSalePrices.get(0);
                    if (seriesTripScheduleCustomSalePrice != null) {
                        BigDecimal customerPrice = seriesTripScheduleCustomSalePrice.getGroupCustomerPrice();
                        BigDecimal groupAgencyPrice = seriesTripScheduleCustomSalePrice.getGroupAgencyPrice();
//                    	BigDecimal customerPrice = calcSalePriceInfoOfflineSelf(tripScheduleDTO.getId(), tokenUser.getId(), seriesTripScheduleCustomSalePrice.getGroupCustomerPrice());
//                        BigDecimal customerPrice = new BigDecimal("0.00");
//                        if (incrementalPriceType == 1) {
//                            customerPrice = seriesTripScheduleCustomSalePrice.getGroupCustomerPrice().add(incrementalPrice);
//                        }
//                        if (incrementalPriceType == 0) {
//                            customerPrice = seriesTripScheduleCustomSalePrice.getGroupCustomerPrice().subtract(incrementalPrice);
//                        }
//                        if (incrementalPriceType == 2) {
//                            customerPrice = seriesTripScheduleCustomSalePrice.getGroupCustomerPrice();
//                        }
                        tripScheduleDTO.setGroupCustomerPrice(customerPrice);
                        tripScheduleDTO.setGroupAgencyPrice(groupAgencyPrice);
////                        tripScheduleDTO.setGroupCustomerPrice(seriesTripScheduleCustomSalePrice.getGroupCustomerPrice());
//                        tripScheduleDTO.setCustomerGrossProfitRate(seriesTripScheduleCustomSalePrice.getCustomerGrossProfitRate());
//                        tripScheduleDTO.setGroupAgencyPrice(seriesTripScheduleCustomSalePrice.getGroupAgencyPrice());
//                        tripScheduleDTO.setAgencyGrossProfitRate(seriesTripScheduleCustomSalePrice.getAgencyGrossProfitRate());
                    }
                }
            }
        }

        return newPageResult(iPage, scheduleDTOList);
    }

    @Override
    public boolean scheduleAgent(TripScheduleAgentReq tripScheduleAgentReq, TokenUser tokenUser) {

        SeriesTripSchedule seriesTripSchedule = baseMapper.selectById(tripScheduleAgentReq.getScheduleId());

        if (null != seriesTripSchedule.getAgentSalePermissionType() && seriesTripSchedule.getAgentSalePermissionType() == 2) {
            return true;
        }
        if (null != seriesTripSchedule.getAgentSalePermissionType() && seriesTripSchedule.getAgentSalePermissionType() == 1) {
            return false;
        }
        Integer total = agentCustomMapper.selectCount(new QueryWrapper<SeriesTripScheduleChannelOfflineAgentCustom>().lambda().eq(SeriesTripScheduleChannelOfflineAgentCustom::getAgentId, tripScheduleAgentReq.getAgentId()).eq(SeriesTripScheduleChannelOfflineAgentCustom::getScheduleId, tripScheduleAgentReq.getScheduleId()));
        if (total > 0) {
            return true;
        }
        return false;
    }

    private void tripScheduleCustomCost(AddTripScheduleReq addTripScheduleReq, Long scheduleId) {
//        修改自定义年龄段属性值
        TripCustomTreeReq tripCustomTree = addTripScheduleReq.getTripCustomTree();
        List<TripCustomTreePassengerReq> passengerList = tripCustomTree.getPassengerList();
        List<SeriesTripScheduleTouristType> seriesTripTouristTypeList = EntityUtil.copyList(passengerList, SeriesTripScheduleTouristType.class);
        for (SeriesTripScheduleTouristType seriesTripScheduleTouristType : seriesTripTouristTypeList) {
            seriesTripScheduleTouristTypeService.update(seriesTripScheduleTouristType, new QueryWrapper<SeriesTripScheduleTouristType>().lambda().eq(SeriesTripScheduleTouristType::getScheduleId, scheduleId).eq(SeriesTripScheduleTouristType::getMainDataId, seriesTripScheduleTouristType.getMainDataId()));
            SeriesTripScheduleTouristType scheduleTouristType = seriesTripScheduleTouristTypeService.getOne(new QueryWrapper<SeriesTripScheduleTouristType>().lambda().eq(SeriesTripScheduleTouristType::getScheduleId, scheduleId).eq(SeriesTripScheduleTouristType::getMainDataId, seriesTripScheduleTouristType.getMainDataId()));
            seriesTripScheduleTouristType.setId(scheduleTouristType.getId());
        }
//        seriesTripScheduleTouristTypeService.updateBatchById(seriesTripTouristTypeList);
//        seriesTripScheduleTouristTypeService.saveOrUpdateBatch(seriesTripTouristTypeList);
        HashMap<String, SeriesTripScheduleTouristType> tripTouristTypeHashMap = new HashMap<>(seriesTripTouristTypeList.size());
        for (SeriesTripScheduleTouristType seriesTripScheduleTouristType : seriesTripTouristTypeList) {
            String key = seriesTripScheduleTouristType.getMainDataId() + ":" + seriesTripScheduleTouristType.getAgeBegin() + seriesTripScheduleTouristType.getAgeEnd();
            tripTouristTypeHashMap.put(key, seriesTripScheduleTouristType);
        }

        //自定义日期段
        List<TripCustomTreeDateReq> dateList = tripCustomTree.getDateList();
        List<SeriesTripScheduleCustomDateSubsection> customDateSubsectionList = EntityUtil.copyList(dateList, SeriesTripScheduleCustomDateSubsection.class);
        customDateSubsectionList.forEach(dateSubsection -> {
            dateSubsection.setScheduleId(scheduleId);
        });
        customDateSubsectionService.saveOrUpdateBatch(customDateSubsectionList);
        HashMap<String, SeriesTripScheduleCustomDateSubsection> customDateSubsectionHashMap = new HashMap<>(customDateSubsectionList.size());
        for (SeriesTripScheduleCustomDateSubsection seriesTripScheduleCustomDateSubsection : customDateSubsectionList) {
            String key = seriesTripScheduleCustomDateSubsection.getSubsectionId() + ":" + seriesTripScheduleCustomDateSubsection.getDateType() + seriesTripScheduleCustomDateSubsection.getCheckWeekDays() + seriesTripScheduleCustomDateSubsection.getDateBegin() + seriesTripScheduleCustomDateSubsection.getDateEnd();
            customDateSubsectionHashMap.put(key, seriesTripScheduleCustomDateSubsection);
        }


        TripCustomCostReq tripCustomCostReq = addTripScheduleReq.getCustomCost();
        //自定义成本费表
        List<SeriesTripScheduleCustomCost> customCostList = new ArrayList<>();
        //销售价格信息
        List<SeriesTripScheduleCustomSalePrice> customSalePriceList = new ArrayList<>();
        //团综合成本
        List<SeriesTripScheduleCustomCostGroup> customCostGroupList = new ArrayList<>();
        //团综合成本自定义值
        List<SeriesTripScheduleCustomCostGroupAge> customCostGroupAgeList = new ArrayList<>();
        //代收费用
        List<SeriesTripScheduleCustomCostCollection> customCostCollectionList = new ArrayList<>();
        //代收费用旅客类型
        List<SeriesTripScheduleCustomTouristType> customCostCollectionTouristTypeList = new ArrayList<>();
        // 调整项目
        List<SeriesTripScheduleCustomFeeAdjust> customFeeAdjustList = new ArrayList<>();
        //调整项目自定义值
        List<SeriesTripScheduleCustomAdjustTourist> customFeeAdjustTouristTypeList = new ArrayList<>();

        //成本主表
        SeriesTripScheduleCustomCost seriesTripCustomCost = new SeriesTripScheduleCustomCost();
        BeanUtils.copyProperties(tripCustomCostReq, seriesTripCustomCost);
        Long costId = IdWorker.getId();
        seriesTripCustomCost.setScheduleId(scheduleId);
        seriesTripCustomCost.setId(costId);
        customCostList.add(seriesTripCustomCost);
        //销售价格设置
        List<TripCustomSalePriceReq> salePriceList = tripCustomCostReq.getCustomSalePriceList();

        //团综合成本
        List<SeriesTripCustomCostGroupReq> costGroupList = tripCustomCostReq.getCostGroupList();

        HashMap<String, BigDecimal> costGroupPriceMap = new HashMap<>();
        SeriesTripScheduleCustomCostGroup seriesTripCustomCostGroup = null;
        for (SeriesTripCustomCostGroupReq seriesTripCustomCostGroupReq : costGroupList) {
            seriesTripCustomCostGroup = new SeriesTripScheduleCustomCostGroup();
            Long costGroupId = IdWorker.getId();
            BeanUtils.copyProperties(seriesTripCustomCostGroupReq, seriesTripCustomCostGroup);
            seriesTripCustomCostGroup.setId(costGroupId);
            seriesTripCustomCostGroup.setCostId(costId);
            seriesTripCustomCostGroup.setScheduleId(scheduleId);
//            seriesTripCustomCostGroup.setTripId(tripId);
            customCostGroupList.add(seriesTripCustomCostGroup);
            //团综合成本自定义值表
            List<CustomCostReq> customValueList = seriesTripCustomCostGroupReq.getCustomCostList();
            if (CollectionUtils.isNotEmpty(customValueList)) {
                SeriesTripScheduleCustomCostGroupAge seriesTripCustomCostGroupAge = null;
                for (CustomCostReq customCostReq : customValueList) {
                    seriesTripCustomCostGroupAge = new SeriesTripScheduleCustomCostGroupAge();
                    seriesTripCustomCostGroupAge.setGroupCostId(costGroupId);
//                    seriesTripCustomCostGroupAge.setCostId(costId);
                    seriesTripCustomCostGroupAge.setScheduleId(scheduleId);
                    seriesTripCustomCostGroupAge.setPrice(customCostReq.getPrice());
                    String dateKey = customCostReq.getSubsectionId() + ":" + customCostReq.getDateType() + customCostReq.getCheckWeekDays() + customCostReq.getDateBegin() + customCostReq.getDateEnd();
                    SeriesTripScheduleCustomDateSubsection customDateSubsection = customDateSubsectionHashMap.get(dateKey);
                    if (customDateSubsection != null) {
                        seriesTripCustomCostGroupAge.setDateSubsectionId(customDateSubsection.getId());
                    }
                    String typeKey = customCostReq.getMainDataId() + ":" + customCostReq.getAgeBegin() + customCostReq.getAgeEnd();
                    SeriesTripScheduleTouristType seriesTripTouristType = tripTouristTypeHashMap.get(typeKey);
                    if (seriesTripTouristType != null) {
                        seriesTripCustomCostGroupAge.setTripTouristTypeId(seriesTripTouristType.getId());
                    }
                    if (customDateSubsection != null && seriesTripTouristType != null) {
                        BigDecimal price = customCostReq.getPrice();
                        String costGroupKey = dateKey + ":" + typeKey;
                        if (costGroupPriceMap.get(costGroupKey) != null) {
                            BigDecimal costPrice = costGroupPriceMap.get(costGroupKey).add(price);
                            costGroupPriceMap.put(costGroupKey, costPrice);
                        } else {
                            costGroupPriceMap.put(costGroupKey, price);
                        }
                    }

                    customCostGroupAgeList.add(seriesTripCustomCostGroupAge);
                }
            }

        }

        if (CollectionUtils.isNotEmpty(salePriceList)) {
            SeriesTripScheduleCustomSalePrice tripCustomSalePrice = null;
            for (TripCustomSalePriceReq tripCustomSalePriceReq : salePriceList) {
                tripCustomSalePrice = new SeriesTripScheduleCustomSalePrice();
                BeanUtils.copyProperties(tripCustomSalePriceReq, tripCustomSalePrice);
                String dateKey = tripCustomSalePriceReq.getSubsectionId() + ":" + tripCustomSalePriceReq.getDateType() + tripCustomSalePriceReq.getCheckWeekDays() + tripCustomSalePriceReq.getDateBegin() + tripCustomSalePriceReq.getDateEnd();
                SeriesTripScheduleCustomDateSubsection customDateSubsection = customDateSubsectionHashMap.get(dateKey);
                if (customDateSubsection != null) {
                    tripCustomSalePrice.setDateSubsectionId(customDateSubsection.getId());
                }
                String typeKey = tripCustomSalePriceReq.getMainDataId() + ":" + tripCustomSalePriceReq.getAgeBegin() + tripCustomSalePriceReq.getAgeEnd();
                SeriesTripScheduleTouristType seriesTripTouristType = tripTouristTypeHashMap.get(typeKey);
                if (seriesTripTouristType != null) {
                    tripCustomSalePrice.setTripTouristTypeId(seriesTripTouristType.getId());
                }

                String costGroupKey = dateKey + ":" + typeKey;
                if (costGroupPriceMap.get(costGroupKey) != null) {
                    BigDecimal costPrice = costGroupPriceMap.get(costGroupKey);
                    tripCustomSalePrice.setGroupComprehensiveCost(costPrice);
                    BigDecimal groupCustomerPrice = tripCustomSalePriceReq.getGroupCustomerPrice().subtract(costPrice);
                    Double customerGrossProfitRate = groupCustomerPrice.divide(tripCustomSalePrice.getGroupCustomerPrice(), RoundingMode.CEILING).doubleValue();
                    tripCustomSalePrice.setCustomerGrossProfitRate(customerGrossProfitRate);

                    BigDecimal groupAgencyPrice = tripCustomSalePriceReq.getGroupAgencyPrice().subtract(costPrice);
                    Double agencyGrossProfitRate = groupAgencyPrice.divide(tripCustomSalePrice.getGroupAgencyPrice(), RoundingMode.CEILING).doubleValue();
                    tripCustomSalePrice.setAgencyGrossProfitRate(agencyGrossProfitRate);
                }


                tripCustomSalePrice.setCostId(costId);
                tripCustomSalePrice.setScheduleId(scheduleId);
                customSalePriceList.add(tripCustomSalePrice);
            }
        }
        //代收费用
        List<SeriesTripDefaultCostCollectionReq> costCollectionList = tripCustomCostReq.getCostCollectionList();
        if (CollectionUtils.isNotEmpty(costCollectionList)) {
            for (SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq : costCollectionList) {
                Long costCollectionId = IdWorker.getId();
                seriesTripDefaultCostCollectionReq.setCostId(costId);
//                seriesTripDefaultCostCollectionReq.setTripId(tripId);
                seriesTripDefaultCostCollectionReq.setId(costCollectionId);
                List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = seriesTripDefaultCostCollectionReq.getCostCollectionTypeList();
                List<SeriesTripScheduleCustomTouristType> touristTypeList = EntityUtil.copyList(costCollectionTypeList, SeriesTripScheduleCustomTouristType.class);
                touristTypeList.forEach(touristType -> {
                    touristType.setCollectionId(costCollectionId);
                    touristType.setScheduleId(scheduleId);
//                    touristType.setTripId(tripId);
                });
                customCostCollectionTouristTypeList.addAll(touristTypeList);
            }
            List<SeriesTripScheduleCustomCostCollection> collectionList = EntityUtil.copyList(costCollectionList, SeriesTripScheduleCustomCostCollection.class);
            collectionList.forEach(costCollection -> {
                costCollection.setCostId(costId);
                costCollection.setScheduleId(scheduleId);
//                costCollection.setTripId(tripId);
            });
            customCostCollectionList.addAll(collectionList);
        }

        //自定义调整项目
        List<SeriesTripCustomFeeAdjustReq> feeAdjustList = tripCustomCostReq.getFeeAdjustList();
        if (Assist.isNotEmpty(feeAdjustList)) {
            SeriesTripScheduleCustomFeeAdjust seriesTripCustomFeeAdjust = null;
            for (SeriesTripCustomFeeAdjustReq seriesTripCustomFeeAdjustReq : feeAdjustList) {
                seriesTripCustomFeeAdjust = new SeriesTripScheduleCustomFeeAdjust();
                Long feeAdjustId = IdWorker.getId();
                BeanUtils.copyProperties(seriesTripCustomFeeAdjustReq, seriesTripCustomFeeAdjust);
                seriesTripCustomFeeAdjust.setId(feeAdjustId);
                seriesTripCustomFeeAdjust.setCostId(costId);
                seriesTripCustomFeeAdjust.setScheduleId(scheduleId);
//            seriesTripCustomFeeAdjust.setTripId(tripId);
                customFeeAdjustList.add(seriesTripCustomFeeAdjust);
                //自定义成本值表
                List<CustomCostReq> customValueList = seriesTripCustomFeeAdjustReq.getCustomCostList();
                if (CollectionUtils.isNotEmpty(customValueList)) {
                    SeriesTripScheduleCustomAdjustTourist seriesTripCustomFeeAdjustTouristType = null;
                    for (CustomCostReq customCostReq : customValueList) {
                        seriesTripCustomFeeAdjustTouristType = new SeriesTripScheduleCustomAdjustTourist();
                        seriesTripCustomFeeAdjustTouristType.setAdjustId(feeAdjustId);
                        seriesTripCustomFeeAdjustTouristType.setCostId(costId);
                        seriesTripCustomFeeAdjustTouristType.setPrice(customCostReq.getPrice());
                        String dateKey = customCostReq.getSubsectionId() + ":" + customCostReq.getDateType() + customCostReq.getCheckWeekDays() + customCostReq.getDateBegin() + customCostReq.getDateEnd();
                        SeriesTripScheduleCustomDateSubsection customDateSubsection = customDateSubsectionHashMap.get(dateKey);
                        if (customDateSubsection != null) {
                            seriesTripCustomFeeAdjustTouristType.setDateSubsectionId(customDateSubsection.getId());
                        }
                        String typeKey = customCostReq.getMainDataId() + ":" + customCostReq.getAgeBegin() + customCostReq.getAgeEnd();
                        SeriesTripScheduleTouristType seriesTripTouristType = tripTouristTypeHashMap.get(typeKey);
                        if (customDateSubsection != null) {
                            seriesTripCustomFeeAdjustTouristType.setTripTouristTypeId(seriesTripTouristType.getId());
                        }
                        customFeeAdjustTouristTypeList.add(seriesTripCustomFeeAdjustTouristType);
                    }
                }
            }
        }


        customCostService.saveBatch(customCostList);
        customSalePriceService.saveBatch(customSalePriceList);
        customCostCollectionService.saveBatch(customCostCollectionList);
        customTouristTypeService.saveBatch(customCostCollectionTouristTypeList);
        customCostGroupService.saveBatch(customCostGroupList);
        customCostGroupAgeService.saveBatch(customCostGroupAgeList);
        customFeeAdjustService.saveBatch(customFeeAdjustList);
        customAdjustTouristService.saveBatch(customFeeAdjustTouristTypeList);
    }

    private void tripScheduleDefaultCost(AddTripScheduleReq addTripScheduleReq, Long scheduleId, Map<Long, MainDataTouristTypeListDTO> mainDataTouristTypeMap) {
        //代收费用旅客类型
        List<SeriesTripScheduleDefaultCostTouristType> seriesTripDefaultCostCollectionTouristTypeList = new ArrayList<>();
        TripScheduleDefaultCostReq tripScheduleDefaultCost = addTripScheduleReq.getTripScheduleDefaultCost();
        SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCost = new SeriesTripScheduleDefaultCost();
        Long tripId = addTripScheduleReq.getTripId();
        Long costId = IdWorker.getId();
        SeriesTripScheduleSellingPriceReq sellingPriceReq = tripScheduleDefaultCost.getSellingPriceReq();
        BeanUtils.copyProperties(sellingPriceReq, seriesTripScheduleDefaultCost);
        seriesTripScheduleDefaultCost.setId(costId);
        seriesTripScheduleDefaultCost.setTripId(tripId);
        seriesTripScheduleDefaultCost.setName(sellingPriceReq.getName());
        seriesTripScheduleDefaultCost.setScheduleId(scheduleId);
        seriesTripScheduleDefaultCost.setIsDeleted(IsDeletedCode.NO.getCode());

        //团综合成本
        List<SeriesTripScheduleDefaultCostGroupReq> costGroupList = tripScheduleDefaultCost.getCostGroupList();
        List<SeriesTripScheduleDefaultCostGroup> defaultCostGroupList = EntityUtil.copyList(costGroupList, SeriesTripScheduleDefaultCostGroup.class);
        BigDecimal defaultGroupPrice = new BigDecimal(0);
        for (SeriesTripScheduleDefaultCostGroup seriesTripScheduleDefaultCostGroup : defaultCostGroupList) {
            seriesTripScheduleDefaultCostGroup.setCostId(costId);
            seriesTripScheduleDefaultCostGroup.setTripId(tripId);
            seriesTripScheduleDefaultCostGroup.setScheduleId(scheduleId);
            defaultGroupPrice = defaultGroupPrice.add(seriesTripScheduleDefaultCostGroup.getCost());
        }
        BigDecimal groupCustomerPrice = seriesTripScheduleDefaultCost.getGroupCustomerPrice().subtract(defaultGroupPrice);
        Double customerGrossProfitRate = groupCustomerPrice.divide(seriesTripScheduleDefaultCost.getGroupCustomerPrice(), RoundingMode.CEILING).doubleValue();
        seriesTripScheduleDefaultCost.setCustomerGrossProfitRate(customerGrossProfitRate);

        BigDecimal groupAgencyPrice = seriesTripScheduleDefaultCost.getGroupAgencyPrice().subtract(defaultGroupPrice);
        Double agencyGrossProfitRate = groupAgencyPrice.divide(seriesTripScheduleDefaultCost.getGroupAgencyPrice(), RoundingMode.CEILING).doubleValue();
        seriesTripScheduleDefaultCost.setAgencyGrossProfitRate(agencyGrossProfitRate);


        seriesTripScheduleDefaultCost.setGroupComprehensiveCost(defaultGroupPrice);
        seriesTripScheduleDefaultCostMapper.insert(seriesTripScheduleDefaultCost);
        List<SeriesTripScheduleDefaultCostGroup> seriesTripDefaultCostGroupList = new ArrayList<>(defaultCostGroupList);
        seriesTripScheduleDefaultCostGroupMapper.insertBatchSomeColumn(seriesTripDefaultCostGroupList);
        //代收费用
        List<SeriesTripScheduleDefaultCostCollectionReq> costCollectionList = tripScheduleDefaultCost.getCostCollectionList();
        if (Assist.isNotEmpty(costCollectionList)) {
            for (SeriesTripScheduleDefaultCostCollectionReq seriesTripScheduleDefaultCostCollectionReq : costCollectionList) {
                Long costCollectionId = IdWorker.getId();
                seriesTripScheduleDefaultCostCollectionReq.setCostId(costId);
                seriesTripScheduleDefaultCostCollectionReq.setTripId(tripId);
                seriesTripScheduleDefaultCostCollectionReq.setId(costCollectionId);
                List<SeriesTripScheduleDefaultCostTouristTypeReq> costCollectionTypeList = seriesTripScheduleDefaultCostCollectionReq.getCostCollectionTypeList();
                if (CollectionUtils.isNotEmpty(costCollectionTypeList)) {
                    List<SeriesTripScheduleDefaultCostTouristType> touristTypeList = EntityUtil.copyList(costCollectionTypeList, SeriesTripScheduleDefaultCostTouristType.class);
                    touristTypeList.forEach(touristType -> {
                        touristType.setCollectionId(costCollectionId);
                        touristType.setTripId(tripId);
                        touristType.setScheduleId(scheduleId);
                        touristType.setId(IdWorker.getId());
                        touristType.setIsDeleted(IsDeletedCode.NO.getCode());
                        MainDataTouristTypeListDTO mainDataTouristType = mainDataTouristTypeMap.get(touristType.getTouristTypeId());
                        if (mainDataTouristType != null) {
                            touristType.setAgeBegin(mainDataTouristType.getAgeBegin());
                            touristType.setAgeEnd(mainDataTouristType.getAgeEnd());
                        }
                    });
                    seriesTripDefaultCostCollectionTouristTypeList.addAll(touristTypeList);
                }
            }
        }
        if (CollectionUtils.isNotEmpty(seriesTripDefaultCostCollectionTouristTypeList)) {
            seriesTripScheduleDefaultCostTouristTypeMapper.insertBatchSomeColumn(seriesTripDefaultCostCollectionTouristTypeList);
        }
        if (Assist.isNotEmpty(costCollectionList)) {
            List<SeriesTripScheduleDefaultCostCollection> defaultCostCollectionList = EntityUtil.copyList(costCollectionList, SeriesTripScheduleDefaultCostCollection.class);
            defaultCostCollectionList.forEach(costCollection -> {
                costCollection.setCostId(costId);
                costCollection.setTripId(tripId);
                costCollection.setScheduleId(scheduleId);
            });
            List<SeriesTripScheduleDefaultCostCollection> seriesTripDefaultCostCollectionList = new ArrayList<>(defaultCostCollectionList);
            seriesTripScheduleDefaultCostCollectionMapper.insertBatchSomeColumn(defaultCostCollectionList);
        }

        //调整项目
        List<SeriesTripScheduleDefaultFeeAdjustReq> feeAdjustList = tripScheduleDefaultCost.getFeeAdjustList();
        if (Assist.isNotEmpty(feeAdjustList)) {
            List<SeriesTripScheduleDefaultFeeAdjust> defaultFeeAdjustList = EntityUtil.copyList(feeAdjustList, SeriesTripScheduleDefaultFeeAdjust.class);
            defaultFeeAdjustList.forEach(feeAdjust -> {
                feeAdjust.setCostId(costId);
                feeAdjust.setTripId(tripId);
                feeAdjust.setScheduleId(scheduleId);
            });
            List<SeriesTripScheduleDefaultFeeAdjust> seriesTripDefaultFeeAdjustList = new ArrayList<>(defaultFeeAdjustList);
            seriesTripScheduleDefaultFeeAdjustMapper.insertBatchSomeColumn(seriesTripDefaultFeeAdjustList);
        }
    }

    private void tripScheduleDefaultCostUpdate(UpdateTripScheduleReq updateTripScheduleReq, Long scheduleId, TokenUser tokenUser) {
        //TODO
        seriesTripScheduleDefaultCostMapper.deleteByScheduleId(scheduleId);
        seriesTripScheduleDefaultCostGroupMapper.deleteByScheduleId(scheduleId);
        seriesTripScheduleDefaultCostTouristTypeMapper.deleteByScheduleId(scheduleId);
        seriesTripScheduleDefaultCostCollectionMapper.deleteByScheduleId(scheduleId);
        seriesTripScheduleDefaultFeeAdjustMapper.deleteByScheduleId(scheduleId);
        //seriesTripScheduleDefaultCostMapper.delete(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, scheduleId));
        //seriesTripScheduleDefaultCostGroupMapper.delete(new QueryWrapper<SeriesTripScheduleDefaultCostGroup>().lambda().eq(SeriesTripScheduleDefaultCostGroup::getScheduleId, scheduleId));
        //seriesTripScheduleDefaultCostTouristTypeMapper.delete(new QueryWrapper<SeriesTripScheduleDefaultCostTouristType>().lambda().eq(SeriesTripScheduleDefaultCostTouristType::getScheduleId, scheduleId));
        //seriesTripScheduleDefaultCostCollectionMapper.delete(new QueryWrapper<SeriesTripScheduleDefaultCostCollection>().lambda().eq(SeriesTripScheduleDefaultCostCollection::getScheduleId, scheduleId));
        //seriesTripScheduleDefaultFeeAdjustMapper.delete(new QueryWrapper<SeriesTripScheduleDefaultFeeAdjust>().lambda().eq(SeriesTripScheduleDefaultFeeAdjust::getScheduleId, scheduleId));

        //查询旅客类型主数据列表
        Map<Long, MainDataTouristTypeListDTO> mainDataTouristTypeMap = queryMainDataTouristTypeList();

        //代收费用旅客类型
        List<SeriesTripScheduleDefaultCostTouristType> seriesTripDefaultCostCollectionTouristTypeList = new ArrayList<>();

        TripScheduleDefaultCostReq tripScheduleDefaultCost = updateTripScheduleReq.getTripScheduleDefaultCost();
        SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCost = newCreateEntity(SeriesTripScheduleDefaultCost.class, null, tokenUser);
        Long tripId = updateTripScheduleReq.getTripId();
        Long costId = IdWorker.getId();
        SeriesTripScheduleSellingPriceReq sellingPriceReq = tripScheduleDefaultCost.getSellingPriceReq();
        BeanUtils.copyProperties(sellingPriceReq, seriesTripScheduleDefaultCost);
        seriesTripScheduleDefaultCost.setId(costId);
        seriesTripScheduleDefaultCost.setTripId(tripId);
        seriesTripScheduleDefaultCost.setName(sellingPriceReq.getName());
        seriesTripScheduleDefaultCost.setScheduleId(scheduleId);

        //团综合成本
        List<SeriesTripScheduleDefaultCostGroupReq> costGroupList = tripScheduleDefaultCost.getCostGroupList();
        List<SeriesTripScheduleDefaultCostGroup> defaultCostGroupList = EntityUtil.copyList(costGroupList, SeriesTripScheduleDefaultCostGroup.class);
        BigDecimal defaultGroupPrice = new BigDecimal(0);
        for (SeriesTripScheduleDefaultCostGroup seriesTripScheduleDefaultCostGroup : defaultCostGroupList) {
            seriesTripScheduleDefaultCostGroup.setCostId(costId);
            seriesTripScheduleDefaultCostGroup.setTripId(tripId);
            seriesTripScheduleDefaultCostGroup.setScheduleId(scheduleId);
            defaultGroupPrice = defaultGroupPrice.add(seriesTripScheduleDefaultCostGroup.getCost());
        }
        BigDecimal groupCustomerPrice = seriesTripScheduleDefaultCost.getGroupCustomerPrice().subtract(defaultGroupPrice);
        Double customerGrossProfitRate = groupCustomerPrice.divide(seriesTripScheduleDefaultCost.getGroupCustomerPrice(), RoundingMode.CEILING).doubleValue();
        seriesTripScheduleDefaultCost.setCustomerGrossProfitRate(customerGrossProfitRate);
        seriesTripScheduleDefaultCostMapper.insert(seriesTripScheduleDefaultCost);

        BigDecimal groupAgencyPrice = seriesTripScheduleDefaultCost.getGroupAgencyPrice().subtract(defaultGroupPrice);
        Double agencyGrossProfitRate = groupAgencyPrice.divide(seriesTripScheduleDefaultCost.getGroupAgencyPrice(), RoundingMode.CEILING).doubleValue();
        seriesTripScheduleDefaultCost.setAgencyGrossProfitRate(agencyGrossProfitRate);

        List<SeriesTripScheduleDefaultCostGroup> seriesTripDefaultCostGroupList = new ArrayList<>(defaultCostGroupList);
        if (Assist.isNotEmpty(seriesTripDefaultCostGroupList)) {
            seriesTripScheduleDefaultCostGroupMapper.insertBatchSomeColumn(seriesTripDefaultCostGroupList);
        }

        List<SeriesTripScheduleDefaultCostCollection> defaultCostCollectionList = new ArrayList<>();

        //代收费用
        List<SeriesTripScheduleDefaultCostCollectionReq> costCollectionList = tripScheduleDefaultCost.getCostCollectionList();
        for (SeriesTripScheduleDefaultCostCollectionReq seriesTripScheduleDefaultCostCollectionReq : costCollectionList) {
            SeriesTripScheduleDefaultCostCollection costCollection = newCreateEntity(SeriesTripScheduleDefaultCostCollection.class, seriesTripScheduleDefaultCostCollectionReq, tokenUser);
            defaultCostCollectionList.add(costCollection);
            costCollection.setCostId(costId);
            costCollection.setTripId(tripId);
            costCollection.setScheduleId(scheduleId);

            List<SeriesTripScheduleDefaultCostTouristTypeReq> costCollectionTypeList = seriesTripScheduleDefaultCostCollectionReq.getCostCollectionTypeList();
            List<SeriesTripScheduleDefaultCostTouristType> touristTypeList = EntityUtil.copyList(costCollectionTypeList, SeriesTripScheduleDefaultCostTouristType.class);
            touristTypeList.forEach(touristType -> {
                touristType.setCollectionId(costCollection.getId());
                touristType.setTripId(tripId);
                touristType.setScheduleId(scheduleId);
                touristType.setId(IdWorker.getId());
                touristType.setIsDeleted(IsDeletedCode.NO.getCode());
                MainDataTouristTypeListDTO mainDataTouristType = mainDataTouristTypeMap.get(touristType.getTouristTypeId());
                if (mainDataTouristType != null) {
                    touristType.setAgeBegin(mainDataTouristType.getAgeBegin());
                    touristType.setAgeEnd(mainDataTouristType.getAgeEnd());
                }
            });
            seriesTripDefaultCostCollectionTouristTypeList.addAll(touristTypeList);
        }
        if (Assist.isNotEmpty(seriesTripDefaultCostCollectionTouristTypeList)) {
            seriesTripScheduleDefaultCostTouristTypeMapper.insertBatchSomeColumn(seriesTripDefaultCostCollectionTouristTypeList);
        }

//        List<SeriesTripScheduleDefaultCostCollection> defaultCostCollectionList = EntityUtil.copyList(costCollectionList, SeriesTripScheduleDefaultCostCollection.class);
//        fillCreateEntity(defaultCostCollectionList, tokenUser);
//        defaultCostCollectionList.forEach(costCollection -> {
//            costCollection.setCostId(costId);
//            costCollection.setTripId(tripId);
//            costCollection.setScheduleId(scheduleId);
//        });

//        List<SeriesTripScheduleDefaultCostCollection> seriesTripDefaultCostCollectionList = new ArrayList<>(defaultCostCollectionList);
        if (Assist.isNotEmpty(defaultCostCollectionList)) {
            seriesTripScheduleDefaultCostCollectionMapper.insertBatchSomeColumn(defaultCostCollectionList);
        }

        //调整项目
        List<SeriesTripScheduleDefaultFeeAdjustReq> feeAdjustList = tripScheduleDefaultCost.getFeeAdjustList();
        List<SeriesTripScheduleDefaultFeeAdjust> defaultFeeAdjustList = EntityUtil.copyList(feeAdjustList, SeriesTripScheduleDefaultFeeAdjust.class);
        fillCreateEntity(defaultFeeAdjustList, tokenUser);
        defaultFeeAdjustList.forEach(feeAdjust -> {
            feeAdjust.setCostId(costId);
            feeAdjust.setTripId(tripId);
            feeAdjust.setScheduleId(scheduleId);
        });
        List<SeriesTripScheduleDefaultFeeAdjust> seriesTripDefaultFeeAdjustList = new ArrayList<>(defaultFeeAdjustList);
        if (Assist.isNotEmpty(seriesTripDefaultFeeAdjustList)) {
            seriesTripScheduleDefaultFeeAdjustMapper.insertBatchSomeColumn(seriesTripDefaultFeeAdjustList);
        }
    }

    /**
     * 获取当月所有天
     *
     * @param yearReq  年
     * @param monthReq 月
     * @return 当月时间
     */
    public static List<LocalDate> getDayListOfMonth(Integer yearReq, Integer monthReq) {
        List<LocalDate> localDateList = new ArrayList<>();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearReq, monthReq, 0);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String date = String.valueOf(year) + "-" + String.format("%02d", month) + "-" + String.format("%02d", i);
            LocalDate localDate = LocalDate.parse(date);
            localDateList.add(localDate);
        }
        return localDateList;

    }

    /**
     * 按照旅团状态和距离当前日期的天数获取行程旅团
     *
     * @param req
     * @return
     */
    @Override
    public List<Long> getTripScheduleByStatusAndDay(TripSchedulePriceReq req) {
        // TODO 首页分组数据核查
        // req.getTripId() 为null
        if (null != req.getTripId() && req.getTripId().size() > 0) {
            return baseMapper.getTripScheduleByStatusAndDay(req.getStatus(), req.getDays(), req.getTripId());
        }
        return null;
    }

    @Override
    public List<Long> getTripScheduleByCreateTime(Long tripCreateIds) {
        return baseMapper.getTripScheduleByCreateTime(tripCreateIds);
    }

    @Override
    public List<Long> getTripScheduleCount(List<Long> id) {

        return null;
    }

    @Override
    public List<LocalDate> getTripScheduDepartureDateByTripId(Long tripId) {
        LocalDate now = LocalDate.now();
        List<SeriesTripSchedule> seriesTripSchedules = baseMapper.selectList(Wrappers.<SeriesTripSchedule>lambdaQuery().eq(SeriesTripSchedule::getTripId, tripId).eq(SeriesTripSchedule::getIsDeleted, IsDeletedCode.NO.getCode()).ne(SeriesTripSchedule::getSelfSalePermissionType, 1).ge(SeriesTripSchedule::getDepartureDate, now).le(SeriesTripSchedule::getDepartureDate, now.plusMonths(5)));
        return seriesTripSchedules.stream().map(item -> item.getDepartureDate()).collect(Collectors.toList());

    }

    @Override
    public List<ScheduleDetails> getTripScheduByTripId(Long id) {
        return baseMapper.getTripScheduByTripId(id);
    }

    @Override
    public Integer getSchedulePositionNumById(Long id) {
        return baseMapper.getSchedulePositionNumById(id);
    }

    @Override
    public Long findScheduleTripIdById(Long scheduleId) {
        SeriesTripSchedule seriesTripSchedule = baseMapper.selectOne(Wrappers.<SeriesTripSchedule>lambdaQuery()
                .eq(SeriesTripSchedule::getId, scheduleId)
                .eq(SeriesTripSchedule::getIsDeleted, IsDeletedCode.NO.getCode()));
        if (ObjectUtils.isNotEmpty(seriesTripSchedule)) {
            return seriesTripSchedule.getTripId();
        }
        return null;
    }

    /**
     * 导出团期列表
     *
     * @param req
     */
    @Override
    public List<TripScheduleDTO> exportScheduleList(TripScheduleListQueryReq req, TokenUser tokenUser) {
        List<TripScheduleDTO> list = new ArrayList<>();
        //暂时使用列表查询方法，如果后期有性能问题，再进行重写
        PageResponse<TripScheduleDTO> pageResponse = scheduleList(req, tokenUser);
        long pages = calcPages(pageResponse);

        for (int i = 0; i < pages; i++) {
            //第一页
            if (i == 0) {
                Assist.add(list, pageResponse.getRecords());
            } else {  //查询其它页
                req.setPageNo(i + 1);
                pageResponse = scheduleList(req, tokenUser);
                if (pageResponse != null) {
                    Assist.add(list, pageResponse.getRecords());
                }
            }
        }

        return list;
    }

    /**
     * 查询系列团预订可下单的代理商列表
     *
     * @param req
     * @return
     */
    @Override
    public Result<List<SeriesAgentByBookDTO>> querAgentListByBook(SeriesAgentListByBookQueryReq req) {
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.assertExistById(req.getScheduleId());
        //代理禁售
        if (BConst.ONE.equals(seriesTripSchedule.getAgentSalePermissionType())) {
            return newResult();
        }

        SysAgentQueryUsableListReq callReq = new SysAgentQueryUsableListReq();
        if (BConst.TWO.equals(seriesTripSchedule.getAgentSalePermissionType())) {  //所有代理
            callReq.setGroupName(req.getGroupName());
        } else {  //指定代理
            List<Long> agentIdList = agentCustomMapper.queryAgentIdListBySchedule(req.getScheduleId());
            if (Assist.isEmpty(agentIdList)) {
                return newResult();
            }
            callReq.setIdList(agentIdList);
        }

        List<SeriesAgentByBookDTO> dtoList = new ArrayList<>();

        List<SysAgentUsableDTO> list = sysAgentService.queryUsableAgentList(callReq).ok();
        if (Assist.isNotEmpty(list)) {
            for (SysAgentUsableDTO dto : list) {
                SeriesAgentByBookDTO bookDto = new SeriesAgentByBookDTO();
                bookDto.setId(dto.getId());
                bookDto.setGroupCnName(dto.getCnName());
                bookDto.setGroupEnName(dto.getEnName());
                dtoList.add(bookDto);
            }
        }

        return newResult(dtoList);
    }

    /**
     * 自营下单查询团期详情
     *
     * @param id
     * @return
     */
    @Override
    public Result<SeriesTripScheduleDTO> findTripScheduleBySelf(Long id) {
        SeriesTripScheduleDTO dto = validateNotNullFn(getTripScheduleId(id));

        //默认成本
        TripScheduleDefaultCostDTO defaultCostDto = validateNotNullFn(dto.getTripScheduleDefaultCost());
        SeriesTripScheduleSellingPriceDTO sellingPriceDTO = validateNotNullFn(defaultCostDto.getSeriesTripScheduleSellingPriceDTO());
        sellingPriceDTO.getGroupCustomerPrice();

        //查询自营渠道
//        List<SeriesTripScheduleChannelOfflineSelfCustom> selfCustomList = selfCustomMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>().lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, id));

        //自定义成本


        return newResult(dto);
    }


    @Override
    public SeriesTripScheduleDTO findTripScheduleId(FindTripScheduleReq findTripScheduleReq, TokenUser tokenUser) {
        Long id = findTripScheduleReq.getId();
        Integer type = findTripScheduleReq.getType();
        //旅行团基础信息
        SeriesTripScheduleDTO seriesTripScheduleDTO = new SeriesTripScheduleDTO();
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(findTripScheduleReq.getId());
        if (seriesTripSchedule == null) {
            SystemError error = SystemError.SYS_412;
            throw new BusinessException(error);
        }
        BeanUtils.copyProperties(seriesTripSchedule, seriesTripScheduleDTO);
        SeriesTripScheduleDataDTO seriesTripScheduleDataDTO = new SeriesTripScheduleDataDTO();
        BeanUtils.copyProperties(seriesTripSchedule, seriesTripScheduleDataDTO);
        SeriesTrip seriesTrip = seriesTripMapper.assertExistById(seriesTripSchedule.getTripId());
        seriesTripScheduleDataDTO.setTitle(seriesTrip.getTitle());
        seriesTripScheduleDataDTO.setSubtitle(seriesTrip.getSubtitle());
        seriesTripScheduleDataDTO.setTripFeaturesJson(seriesTrip.getTripFeaturesJson());
        seriesTripScheduleDTO.setSeriesTripScheduleDataDTO(seriesTripScheduleDataDTO);
        seriesTripScheduleDTO.setCostSharingType(seriesTripSchedule.getCostSharingType());
        //旅行团规则列表
        SeriesTripScheduleTeamRuleDTO seriesTripScheduleTeamRuleDTO = new SeriesTripScheduleTeamRuleDTO();
        QueryWrapper<SeriesTripScheduleTeamRule> queryWrapper = new QueryWrapper<>();
        SeriesTripScheduleTeamRule seriesTripScheduleTeamRule = seriesTripScheduleTeamRuleMapper.selectOne(queryWrapper.eq("schedule_id", id));
        BeanUtils.copyProperties(seriesTripScheduleTeamRule, seriesTripScheduleTeamRuleDTO);
        //负责人
        seriesTripScheduleTeamRuleDTO.setChargePersonId(seriesTripSchedule.getChargePersonId());
        seriesTripScheduleTeamRuleDTO.setChargePersonName(seriesTripSchedule.getChargePersonName());
        seriesTripScheduleTeamRuleDTO.setChargePersonNumber(seriesTripSchedule.getChargePersonNumber());

        //操作人
        seriesTripScheduleTeamRuleDTO.setOpId(seriesTrip.getOpId());
        seriesTripScheduleTeamRuleDTO.setOpName(seriesTrip.getOpName());
        seriesTripScheduleTeamRuleDTO.setOpNumber(seriesTrip.getOpNumber());

        List<Passenger> passengerList = new ArrayList<>();
        List<SeriesTripScheduleTouristType> seriesTripScheduleTouristTypes = seriesTripScheduleTouristTypeService.list(new QueryWrapper<SeriesTripScheduleTouristType>().lambda().eq(SeriesTripScheduleTouristType::getRuleId, seriesTripScheduleTeamRule.getId()).orderByDesc(SeriesTripScheduleTouristType::getSort));
        if (CollectionUtils.isNotEmpty(seriesTripScheduleTouristTypes)) {
            for (SeriesTripScheduleTouristType seriesTripScheduleTouristType : seriesTripScheduleTouristTypes) {
                Passenger passenger = new Passenger();
                passenger.setId(seriesTripScheduleTouristType.getId());
                passenger.setMainDataId(seriesTripScheduleTouristType.getMainDataId());
                passenger.setName(seriesTripScheduleTouristType.getName());
                passenger.setIsOccupyBed(seriesTripScheduleTouristType.getIsOccupyBed());
                passengerList.add(passenger);
            }
        }
        seriesTripScheduleTeamRuleDTO.setPassenger(passengerList);

        List<SeriesTripScheduleInfoChangeRule> seriesTripScheduleInfoChangeRules = seriesTripScheduleInfoChangeRuleService.list(new QueryWrapper<SeriesTripScheduleInfoChangeRule>().lambda().eq(SeriesTripScheduleInfoChangeRule::getScheduleId, id));
        List<SeriesTripInfoChangeRuleReq> changeRuleList = EntityUtil.copyList(seriesTripScheduleInfoChangeRules, SeriesTripInfoChangeRuleReq.class);
        seriesTripScheduleTeamRuleDTO.setChangeRuleList(changeRuleList);
        seriesTripScheduleDTO.setSeriesTripScheduleTeamRuleDTO(seriesTripScheduleTeamRuleDTO);
        seriesTripScheduleDTO.setAgentSalePermissionType(seriesTripSchedule.getAgentSalePermissionType());
        seriesTripScheduleDTO.setSelfSalePermissionType(seriesTripSchedule.getSelfSalePermissionType());
        //查询自营渠道
        List<SeriesTripScheduleChannelOfflineSelfCustom> selfCustomList = selfCustomMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>().lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, id));
        List<OwnChannelReq> ownChannelList = EntityUtil.copyList(selfCustomList, OwnChannelReq.class);
        seriesTripScheduleDTO.setOwnChannelList(ownChannelList);
        //代理商
        List<SeriesTripScheduleChannelOfflineAgentCustom> agentCustomList = agentCustomMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOfflineAgentCustom>().lambda().eq(SeriesTripScheduleChannelOfflineAgentCustom::getScheduleId, id));
        List<AgentChannelReq> agentChannelList = EntityUtil.copyList(agentCustomList, AgentChannelReq.class);
        seriesTripScheduleDTO.setAgentChannelList(agentChannelList);
        //官方平台
        List<SeriesTripScheduleChannelOnlineSelfPlatform> selfPlatformList = selfPlatformMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOnlineSelfPlatform>().lambda().eq(SeriesTripScheduleChannelOnlineSelfPlatform::getScheduleId, id));
        List<OfficialReq> officialList = EntityUtil.copyList(selfPlatformList, OfficialReq.class);
        seriesTripScheduleDTO.setOfficialList(officialList);
        //第三方平台
        List<SeriesTripScheduleChannelOnlineThirdPlatform> thirdPlatformList = thirdPlatformMapper.selectList(new QueryWrapper<SeriesTripScheduleChannelOnlineThirdPlatform>().lambda().eq(SeriesTripScheduleChannelOnlineThirdPlatform::getScheduleId, id));
        List<OtherPlatformsReq> otherPlatformsList = EntityUtil.copyList(thirdPlatformList, OtherPlatformsReq.class);
        seriesTripScheduleDTO.setOtherPlatformsList(otherPlatformsList);

        BigDecimal incrementalPrice = new BigDecimal("0.00");
        Integer incrementalPriceType = 2;


//        //自营
//        if (type == 1) {
//
//            if (seriesTripSchedule.getSelfSalePermissionType() == 2) {
//                QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom> selfQueryWrapper = new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>();
//                selfQueryWrapper.lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, id)
//                        .eq(SeriesTripScheduleChannelOfflineSelfCustom::getIsDeleted, 0).isNull(SeriesTripScheduleChannelOfflineSelfCustom::getCheckType)
//                        .isNull(SeriesTripScheduleChannelOfflineSelfCustom::getRelationId);
//                SeriesTripScheduleChannelOfflineSelfCustom seriesTripScheduleChannelOfflineSelfCustom = selfCustomMapper.selectOne(selfQueryWrapper);
//                if (null != seriesTripScheduleChannelOfflineSelfCustom) {
//                    incrementalPriceType = seriesTripScheduleChannelOfflineSelfCustom.getAdjustType();
//                    incrementalPrice = incrementalPrice.add(seriesTripScheduleChannelOfflineSelfCustom.getAmount());
//                }
//            }
//            if (seriesTripSchedule.getSelfSalePermissionType() == 3) {
//                QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom> selfQueryWrapper = new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>();
//                selfQueryWrapper.lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, id)
//                        .eq(SeriesTripScheduleChannelOfflineSelfCustom::getIsDeleted, 0).eq(SeriesTripScheduleChannelOfflineSelfCustom::getCheckType, 2)
//                        .eq(SeriesTripScheduleChannelOfflineSelfCustom::getRelationId, tokenUser.getId()).isNotNull(SeriesTripScheduleChannelOfflineSelfCustom::getAdjustType);
//                SeriesTripScheduleChannelOfflineSelfCustom seriesTripScheduleChannelOfflineSelfCustom = selfCustomMapper.selectOne(selfQueryWrapper);
//
//                if (null != seriesTripScheduleChannelOfflineSelfCustom) {
//                    incrementalPriceType = seriesTripScheduleChannelOfflineSelfCustom.getAdjustType();
//                    incrementalPrice = incrementalPrice.add(seriesTripScheduleChannelOfflineSelfCustom.getAmount());
//                } else {
//                    SysDepartmentDTO sysDepartmentDTO = sysDepartmentService.queryByUserId(tokenUser.getId());
//                    QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom> queryDepartmentWrapper = new QueryWrapper<SeriesTripScheduleChannelOfflineSelfCustom>();
//                    queryDepartmentWrapper.lambda().eq(SeriesTripScheduleChannelOfflineSelfCustom::getScheduleId, id)
//                            .eq(SeriesTripScheduleChannelOfflineSelfCustom::getIsDeleted, 0).eq(SeriesTripScheduleChannelOfflineSelfCustom::getCheckType, 1)
//                            .eq(SeriesTripScheduleChannelOfflineSelfCustom::getRelationId, sysDepartmentDTO.getId()).isNotNull(SeriesTripScheduleChannelOfflineSelfCustom::getAdjustType);
//                    SeriesTripScheduleChannelOfflineSelfCustom departmentSelfCustom = selfCustomMapper.selectOne(queryDepartmentWrapper);
//                    if (null != departmentSelfCustom) {
//                        incrementalPriceType = departmentSelfCustom.getAdjustType();
//                        incrementalPrice = incrementalPrice.add(departmentSelfCustom.getAmount());
//                    }
//
//                }
//            }
//        }
//        if (type == 2) {
//            if (seriesTripSchedule.getAgentSalePermissionType() == 2) {
//                QueryWrapper<SeriesTripScheduleChannelOfflineAgentCustom> agentCustomQueryWrapper = new QueryWrapper<SeriesTripScheduleChannelOfflineAgentCustom>();
//                agentCustomQueryWrapper.lambda().eq(SeriesTripScheduleChannelOfflineAgentCustom::getScheduleId, id)
//                        .eq(SeriesTripScheduleChannelOfflineAgentCustom::getIsDeleted, 0);
//                SeriesTripScheduleChannelOfflineAgentCustom seriesTripScheduleChannelOfflineAgentCustom = agentCustomMapper.selectOne(agentCustomQueryWrapper);
//                if (seriesTripScheduleChannelOfflineAgentCustom != null) {
//                    incrementalPriceType = seriesTripScheduleChannelOfflineAgentCustom.getAdjustType();
//                    incrementalPrice = incrementalPrice.add(seriesTripScheduleChannelOfflineAgentCustom.getAmount());
//                }
//            }
//            if (seriesTripSchedule.getAgentSalePermissionType() == 3) {
//                QueryWrapper<SeriesTripScheduleChannelOfflineAgentCustom> agentCustomQueryWrapper = new QueryWrapper<SeriesTripScheduleChannelOfflineAgentCustom>();
//                agentCustomQueryWrapper.lambda().eq(SeriesTripScheduleChannelOfflineAgentCustom::getAgentId, findTripScheduleReq.getAgentId())
//                        .eq(SeriesTripScheduleChannelOfflineAgentCustom::getIsDeleted, 0).eq(SeriesTripScheduleChannelOfflineAgentCustom::getScheduleId, id);
//                SeriesTripScheduleChannelOfflineAgentCustom seriesTripScheduleChannelOfflineAgentCustom = agentCustomMapper.selectOne(agentCustomQueryWrapper);
//                if (seriesTripScheduleChannelOfflineAgentCustom != null) {
//                    incrementalPriceType = seriesTripScheduleChannelOfflineAgentCustom.getAdjustType();
//                    incrementalPrice = incrementalPrice.add(seriesTripScheduleChannelOfflineAgentCustom.getAmount());
//                }
//            }
//        }


        if (seriesTripSchedule.getCostSharingType() == 1) {
            //成本分摊主表
            SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCostDefaultCost = seriesTripScheduleDefaultCostMapper.selectOne(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, id).eq(SeriesTripScheduleDefaultCost::getIsDeleted, IsDeletedCode.NO.getCode()));
            BigDecimal customerPrice = new BigDecimal("0.00");
            //类型为自营，直客价加幅
            if (type == 1) {
                customerPrice = calcSalePriceInfoOfflineSelf(seriesTripSchedule.getId(), tokenUser.getId(), seriesTripScheduleDefaultCostDefaultCost.getGroupCustomerPrice());
//                if (incrementalPriceType == 1) {
//                    customerPrice = seriesTripScheduleDefaultCostDefaultCost.getGroupCustomerPrice().add(incrementalPrice);
//                }
//                if (incrementalPriceType == 0) {
//                    customerPrice = seriesTripScheduleDefaultCostDefaultCost.getGroupCustomerPrice().subtract(incrementalPrice);
//                }
//                if (incrementalPriceType == 2) {
//                    customerPrice = seriesTripScheduleDefaultCostDefaultCost.getGroupCustomerPrice();
//                }
                seriesTripScheduleDefaultCostDefaultCost.setGroupCustomerPrice(customerPrice);
            }
            //类型为代理，代理价加幅
            if (type == 2) {
                customerPrice = calcSalePriceInfoOfflineAgent(seriesTripSchedule.getId(), findTripScheduleReq.getAgentId(), seriesTripScheduleDefaultCostDefaultCost.getGroupAgencyPrice());
//                if (incrementalPriceType == 1) {
//                    customerPrice = seriesTripScheduleDefaultCostDefaultCost.getGroupAgencyPrice().add(incrementalPrice);
//                }
//                if (incrementalPriceType == 0) {
//                    customerPrice = seriesTripScheduleDefaultCostDefaultCost.getGroupAgencyPrice().subtract(incrementalPrice);
//                }
//                if (incrementalPriceType == 2) {
//                    customerPrice = seriesTripScheduleDefaultCostDefaultCost.getGroupAgencyPrice();
//                }
                seriesTripScheduleDefaultCostDefaultCost.setGroupAgencyPrice(customerPrice);
            }

            TripScheduleDefaultCostDTO tripScheduleDefaultCostDTO = new TripScheduleDefaultCostDTO();
            tripScheduleDefaultCostDTO.setId(seriesTripScheduleDefaultCostDefaultCost.getId());
            tripScheduleDefaultCostDTO.setTripId(seriesTripScheduleDefaultCostDefaultCost.getTripId());
            SeriesTripScheduleSellingPriceDTO seriesTripScheduleSellingPriceDTO = new SeriesTripScheduleSellingPriceDTO();
            BeanUtils.copyProperties(seriesTripScheduleDefaultCostDefaultCost, seriesTripScheduleSellingPriceDTO);
            tripScheduleDefaultCostDTO.setSeriesTripScheduleSellingPriceDTO(seriesTripScheduleSellingPriceDTO);
            //团综合成本
            List<SeriesTripScheduleDefaultCostGroup> seriesTripScheduleDefaultCostGroupList = seriesTripScheduleDefaultCostGroupMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostGroup>().lambda().eq(SeriesTripScheduleDefaultCostGroup::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()).eq(SeriesTripScheduleDefaultCostGroup::getIsDeleted, 0));
            if (CollectionUtils.isNotEmpty(seriesTripScheduleDefaultCostGroupList)) {
                List<SeriesTripScheduleDefaultCostGroupDTO> seriesTripDefaultCostGroupReqList = EntityUtil.copyList(seriesTripScheduleDefaultCostGroupList, SeriesTripScheduleDefaultCostGroupDTO.class);
                tripScheduleDefaultCostDTO.setCostGroupList(seriesTripDefaultCostGroupReqList);
            }
            //调整费用
            List<SeriesTripScheduleDefaultFeeAdjust> defaultFeeAdjustList = seriesTripScheduleDefaultFeeAdjustMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultFeeAdjust>().lambda().eq(SeriesTripScheduleDefaultFeeAdjust::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()).eq(SeriesTripScheduleDefaultFeeAdjust::getIsDeleted, 0));
            if (CollectionUtils.isNotEmpty(defaultFeeAdjustList)) {
                List<SeriesTripScheduleDefaultFeeAdjustDTO> feeAdjustList = EntityUtil.copyList(defaultFeeAdjustList, SeriesTripScheduleDefaultFeeAdjustDTO.class);
                tripScheduleDefaultCostDTO.setFeeAdjustList(feeAdjustList);
            }
            //代收费用
            List<SeriesTripScheduleDefaultCostCollection> defaultCostCollectionList = seriesTripScheduleDefaultCostCollectionMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostCollection>().lambda().eq(SeriesTripScheduleDefaultCostCollection::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()).eq(SeriesTripScheduleDefaultCostCollection::getIsDeleted, 0));
            if (CollectionUtils.isNotEmpty(defaultCostCollectionList)) {
                List<SeriesTripScheduleDefaultCostCollectionDTO> costCollectionList = new ArrayList<>();
                for (SeriesTripScheduleDefaultCostCollection seriesTripScheduleDefaultCostCollection : defaultCostCollectionList) {
                    SeriesTripScheduleDefaultCostCollectionDTO seriesTripScheduleDefaultCostCollectionDTO = new SeriesTripScheduleDefaultCostCollectionDTO();
                    BeanUtils.copyProperties(seriesTripScheduleDefaultCostCollection, seriesTripScheduleDefaultCostCollectionDTO);
                    List<SeriesTripScheduleDefaultCostTouristType> defaultCostCollectionTouristTypeList = seriesTripScheduleDefaultCostTouristTypeMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultCostTouristType>().lambda().eq(SeriesTripScheduleDefaultCostTouristType::getCollectionId, seriesTripScheduleDefaultCostCollection.getId()).eq(SeriesTripScheduleDefaultCostTouristType::getIsDeleted, 0));
                    List<SeriesTripScheduleDefaultCostTouristTypeDTO> costCollectionTypeList = EntityUtil.copyList(defaultCostCollectionTouristTypeList, SeriesTripScheduleDefaultCostTouristTypeDTO.class);
                    seriesTripScheduleDefaultCostCollectionDTO.setCostCollectionTypeList(costCollectionTypeList);
                    costCollectionList.add(seriesTripScheduleDefaultCostCollectionDTO);
                }
                tripScheduleDefaultCostDTO.setCostCollectionList(costCollectionList);
            }

            seriesTripScheduleDTO.setTripScheduleDefaultCost(tripScheduleDefaultCostDTO);
        }

        if (seriesTripSchedule.getCostSharingType() == 2) {
            TripCustomTreeReq tripCustomTree = new TripCustomTreeReq();
            List<SeriesTripScheduleTouristType> seriesTripTouristTypeList = seriesTripScheduleTouristTypeService.list(new QueryWrapper<SeriesTripScheduleTouristType>().lambda().eq(SeriesTripScheduleTouristType::getScheduleId, id).isNotNull(SeriesTripScheduleTouristType::getAgeBegin).orderByDesc(SeriesTripScheduleTouristType::getSort));
            List<TripCustomTreePassengerReq> touristTypeList = EntityUtil.copyList(seriesTripTouristTypeList, TripCustomTreePassengerReq.class);
            tripCustomTree.setPassengerList(touristTypeList);
            List<SeriesTripScheduleCustomDateSubsection> seriesTripCustomDateSubsectionList = customDateSubsectionService.list(new QueryWrapper<SeriesTripScheduleCustomDateSubsection>().lambda().eq(SeriesTripScheduleCustomDateSubsection::getScheduleId, id));
            List<TripCustomTreeDateReq> dateList = EntityUtil.copyList(seriesTripCustomDateSubsectionList, TripCustomTreeDateReq.class);
            tripCustomTree.setDateList(dateList);
            seriesTripScheduleDTO.setTripCustomTree(tripCustomTree);

            //成本分摊主表
//            SeriesTripScheduleCustomCost seriesTripCustomCost = customCostService.getOne(new QueryWrapper<SeriesTripScheduleCustomCost>().lambda().eq(SeriesTripScheduleCustomCost::getScheduleId, id));
            SeriesTripScheduleCustomCost seriesTripCustomCost = seriesTripScheduleCustomCostMapper.assertExistSelectOneByKey("schedule_id", id);
            TripCustomCostReq tripCustomCostReq = null;
            tripCustomCostReq = new TripCustomCostReq();

            BeanUtils.copyProperties(seriesTripCustomCost, tripCustomCostReq);
            tripCustomCostReq.setId(seriesTripCustomCost.getId());
            tripCustomCostReq.setTripId(seriesTripCustomCost.getTripId());
            List<SeriesTripScheduleCustomSalePrice> seriesTripCustomSalePriceList = customSalePriceService.list(new QueryWrapper<SeriesTripScheduleCustomSalePrice>().lambda().eq(SeriesTripScheduleCustomSalePrice::getCostId, seriesTripCustomCost.getId()));
            List<TripCustomSalePriceReq> customSalePriceList = new ArrayList<>();
            for (SeriesTripScheduleCustomSalePrice seriesTripCustomSalePrice : seriesTripCustomSalePriceList) {
                //类型为自营，直客价加幅
                BigDecimal customerPrice = new BigDecimal("0.00");
                if (type == 1) {
                    if (incrementalPriceType == 1) {
                        customerPrice = seriesTripCustomSalePrice.getGroupCustomerPrice().add(incrementalPrice);
                    }
                    if (incrementalPriceType == 0) {
                        customerPrice = seriesTripCustomSalePrice.getGroupCustomerPrice().subtract(incrementalPrice);
                    }
                    if (incrementalPriceType == 2) {
                        customerPrice = seriesTripCustomSalePrice.getGroupCustomerPrice();
                    }
                    seriesTripCustomSalePrice.setGroupCustomerPrice(customerPrice);
                }
                //类型为代理，代理价加幅
                if (type == 2) {
                    if (incrementalPriceType == 1) {
                        customerPrice = seriesTripCustomSalePrice.getGroupAgencyPrice().add(incrementalPrice);
                    }
                    if (incrementalPriceType == 0) {
                        customerPrice = seriesTripCustomSalePrice.getGroupAgencyPrice().subtract(incrementalPrice);
                    }
                    if (incrementalPriceType == 2) {
                        customerPrice = seriesTripCustomSalePrice.getGroupAgencyPrice();
                    }
                    seriesTripCustomSalePrice.setGroupAgencyPrice(customerPrice);
                }
                TripCustomSalePriceReq tripCustomSalePriceReq = new TripCustomSalePriceReq();
                BeanUtils.copyProperties(seriesTripCustomSalePrice, tripCustomSalePriceReq);

                SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomSalePrice.getDateSubsectionId());
                if (seriesTripCustomDateSubsection != null) {
                    tripCustomSalePriceReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                    tripCustomSalePriceReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                    tripCustomSalePriceReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                    tripCustomSalePriceReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                    tripCustomSalePriceReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                    tripCustomSalePriceReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                    tripCustomSalePriceReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                    tripCustomSalePriceReq.setDateId(seriesTripCustomDateSubsection.getId());
                }
                SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomSalePrice.getTripTouristTypeId());
                if (seriesTripTouristType != null) {
                    tripCustomSalePriceReq.setPassengerTypeId(seriesTripTouristType.getId());
                    tripCustomSalePriceReq.setMainDataId(seriesTripTouristType.getMainDataId());
                    tripCustomSalePriceReq.setName(seriesTripTouristType.getName());
                    tripCustomSalePriceReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                    tripCustomSalePriceReq.setAgeEnd(seriesTripTouristType.getAgeEnd());
                }
                customSalePriceList.add(tripCustomSalePriceReq);
            }
            tripCustomCostReq.setCustomSalePriceList(customSalePriceList);

            //团综合成本
            List<SeriesTripScheduleCustomCostGroup> customCostGroupList = customCostGroupService.list(new QueryWrapper<SeriesTripScheduleCustomCostGroup>().lambda().eq(SeriesTripScheduleCustomCostGroup::getCostId, seriesTripCustomCost.getId()));
            if (CollectionUtils.isNotEmpty(customCostGroupList)) {
                List<SeriesTripCustomCostGroupReq> costGroupList = EntityUtil.copyList(customCostGroupList, SeriesTripCustomCostGroupReq.class);

                for (SeriesTripCustomCostGroupReq seriesTripCustomCostGroupReq : costGroupList) {
                    List<CustomCostReq> customCostReqs = new ArrayList<>();
                    List<SeriesTripScheduleCustomCostGroupAge> customCostGroupAgeList = customCostGroupAgeService.list(new QueryWrapper<SeriesTripScheduleCustomCostGroupAge>().lambda().eq(SeriesTripScheduleCustomCostGroupAge::getGroupCostId, seriesTripCustomCostGroupReq.getId()));
                    for (SeriesTripScheduleCustomCostGroupAge seriesTripCustomCostGroupAge : customCostGroupAgeList) {
                        CustomCostReq customCostReq = new CustomCostReq();
                        customCostReq.setPrice(seriesTripCustomCostGroupAge.getPrice());
                        SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomCostGroupAge.getDateSubsectionId());
                        if (seriesTripCustomDateSubsection != null) {
                            customCostReq.setDateId(seriesTripCustomDateSubsection.getId());
                            customCostReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                            customCostReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                            customCostReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                            customCostReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                            customCostReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                            customCostReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                        }
                        SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomCostGroupAge.getTripTouristTypeId());
                        if (seriesTripTouristType != null) {
                            customCostReq.setMainDataId(seriesTripTouristType.getMainDataId());
                            customCostReq.setName(seriesTripTouristType.getName());
                            customCostReq.setAgeBegin(seriesTripTouristType.getAgeEnd());
                            customCostReq.setAgeEnd(seriesTripTouristType.getAgeEnd());
                            customCostReq.setPassengerTypeId(seriesTripTouristType.getId());

                        }
                        customCostReqs.add(customCostReq);
                    }
                    seriesTripCustomCostGroupReq.setCustomCostList(customCostReqs);
                }

                tripCustomCostReq.setCostGroupList(costGroupList);
            }
            //调整费用
            List<SeriesTripScheduleCustomFeeAdjust> customFeeAdjustList = customFeeAdjustService.list(new QueryWrapper<SeriesTripScheduleCustomFeeAdjust>().lambda().eq(SeriesTripScheduleCustomFeeAdjust::getCostId, seriesTripCustomCost.getId()));
            if (CollectionUtils.isNotEmpty(customFeeAdjustList)) {
                List<SeriesTripCustomFeeAdjustReq> feeAdjustList = EntityUtil.copyList(customFeeAdjustList, SeriesTripCustomFeeAdjustReq.class);

                for (SeriesTripCustomFeeAdjustReq seriesTripCustomFeeAdjustReq : feeAdjustList) {
                    List<CustomCostReq> customCostReqs = new ArrayList<>();
                    List<SeriesTripScheduleCustomAdjustTourist> customFeeAdjustTouristTypeList = customAdjustTouristService.list(new QueryWrapper<SeriesTripScheduleCustomAdjustTourist>().lambda().eq(SeriesTripScheduleCustomAdjustTourist::getAdjustId, seriesTripCustomFeeAdjustReq.getId()));
                    for (SeriesTripScheduleCustomAdjustTourist seriesTripCustomFeeAdjustTouristType : customFeeAdjustTouristTypeList) {
                        CustomCostReq customCostReq = new CustomCostReq();
                        customCostReq.setPrice(seriesTripCustomFeeAdjustTouristType.getPrice());
                        SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomFeeAdjustTouristType.getDateSubsectionId());
                        if (seriesTripCustomDateSubsection != null) {
                            customCostReq.setDateId(seriesTripCustomDateSubsection.getId());
                            customCostReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                            customCostReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                            customCostReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                            customCostReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                            customCostReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                            customCostReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                        }

                        SeriesTripScheduleTouristType seriesTripTouristType = seriesTripScheduleTouristTypeService.getById(seriesTripCustomFeeAdjustTouristType.getTripTouristTypeId());
                        if (seriesTripTouristType != null) {
                            customCostReq.setPassengerTypeId(seriesTripTouristType.getId());
                            customCostReq.setMainDataId(seriesTripTouristType.getMainDataId());
                            customCostReq.setName(seriesTripTouristType.getName());
                            customCostReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                            customCostReq.setAgeEnd(seriesTripTouristType.getAgeEnd());

                        }
                        customCostReqs.add(customCostReq);
                    }
                    seriesTripCustomFeeAdjustReq.setCustomCostList(customCostReqs);
                }

                tripCustomCostReq.setFeeAdjustList(feeAdjustList);
            }
            //代收费用
            List<SeriesTripScheduleCustomCostCollection> customCostCollectionList = customCostCollectionService.list(new QueryWrapper<SeriesTripScheduleCustomCostCollection>().lambda().eq(SeriesTripScheduleCustomCostCollection::getCostId, seriesTripCustomCost.getId()));
            if (CollectionUtils.isNotEmpty(customCostCollectionList)) {
                List<SeriesTripDefaultCostCollectionReq> costCollectionList = new ArrayList<>();
                for (SeriesTripScheduleCustomCostCollection seriesTripCustomCostCollection : customCostCollectionList) {
                    SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq = new SeriesTripDefaultCostCollectionReq();
                    BeanUtils.copyProperties(seriesTripCustomCostCollection, seriesTripDefaultCostCollectionReq);
                    List<SeriesTripScheduleCustomTouristType> customCostCollectionTouristTypeList = customTouristTypeService.list(new QueryWrapper<SeriesTripScheduleCustomTouristType>().lambda().eq(SeriesTripScheduleCustomTouristType::getCollectionId, seriesTripCustomCostCollection.getId()));
                    List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = EntityUtil.copyList(customCostCollectionTouristTypeList, SeriesTripDefaultCostCollectionTypeReq.class);
                    seriesTripDefaultCostCollectionReq.setCostCollectionTypeList(costCollectionTypeList);
                    costCollectionList.add(seriesTripDefaultCostCollectionReq);
                }
                tripCustomCostReq.setCostCollectionList(costCollectionList);
            }

            seriesTripScheduleDTO.setTripCustomCostReq(tripCustomCostReq);
        }

        //可预订余位信息
        if (seriesTripScheduleTeamRuleDTO.getMaxPeople() != null && seriesTripScheduleDataDTO.getLockedTouristNum() != null) {
            int reserveSeatNum = seriesTripScheduleTeamRuleDTO.getMaxPeople() - seriesTripScheduleDataDTO.getLockedTouristNum();
            seriesTripScheduleDTO.setReserveSeatNum(reserveSeatNum);
        }


        return seriesTripScheduleDTO;

    }

    /**
     * 操作订单-调整项目列表
     *
     * @param id
     * @return
     */
    @Override
    public Result<TripScheduleAdjustProjectDTO> adjustProjectList(Long id) {
        TripScheduleAdjustProjectDTO dto = new TripScheduleAdjustProjectDTO();
        SeriesTripSchedule tripSchedule = getById(id);
        if (tripSchedule == null || Objects.equals(tripSchedule.getIsDeleted(), 1)) {
            throw new BusinessException(SystemError.SYS_411);
        }
        dto.setCostSharingType(tripSchedule.getCostSharingType());

        //查询旅客类型主数据列表
        Map<Long, MainDataTouristTypeListDTO> mainDataTouristTypeMap = queryMainDataTouristTypeList();

        //调整项目
        List<FeeAdjustSaveReq> feeAdjustList = new ArrayList<>();
        //成本分摊模板类型
        if (tripSchedule.getCostSharingType() == 1) {
            //成本分摊主表
            SeriesTripScheduleDefaultCost seriesTripScheduleDefaultCostDefaultCost = seriesTripScheduleDefaultCostMapper.selectOne(new QueryWrapper<SeriesTripScheduleDefaultCost>().lambda().eq(SeriesTripScheduleDefaultCost::getScheduleId, id).eq(SeriesTripScheduleDefaultCost::getIsDeleted, IsDeletedCode.NO.getCode()));
            //调整费用
            List<SeriesTripScheduleDefaultFeeAdjust> defaultFeeAdjustList = seriesTripScheduleDefaultFeeAdjustMapper.selectList(new QueryWrapper<SeriesTripScheduleDefaultFeeAdjust>().lambda().eq(SeriesTripScheduleDefaultFeeAdjust::getCostId, seriesTripScheduleDefaultCostDefaultCost.getId()));
            if (CollectionUtils.isNotEmpty(defaultFeeAdjustList)) {
                for (SeriesTripScheduleDefaultFeeAdjust feeAdjust : defaultFeeAdjustList) {
                    FeeAdjustSaveReq feeAdjustSaveReq = EntityUtil.copy(feeAdjust, FeeAdjustSaveReq.class);
                    FeeAdjustSavePriceReq currency = new FeeAdjustSavePriceReq();
                    currency.setCurrencyId(feeAdjust.getCurrencyId());
                    currency.setCurrency(feeAdjust.getCurrency());
                    currency.setExchangeRate(feeAdjust.getExchangeRate());
                    currency.setCostAmount(feeAdjust.getCostAmount());
                    currency.setCostStandardAmount(feeAdjust.getCostAmount().multiply(new BigDecimal(feeAdjust.getExchangeRate())));
                    currency.setTripTouristTypeName("成人");
                    feeAdjustSaveReq.setCurrencyList(Assist.asList(currency));
//                    feeAdjustSaveReq.setCostAmount(feeAdjust.getCostAmount().multiply(new BigDecimal(feeAdjust.getExchangeRate())));
                    feeAdjustList.add(feeAdjustSaveReq);
                }
            }
        } else if (tripSchedule.getCostSharingType() == 2) {
            //成本分摊主表
            SeriesTripScheduleCustomCost seriesTripCustomCost = customCostService.getOne(new QueryWrapper<SeriesTripScheduleCustomCost>().lambda().eq(SeriesTripScheduleCustomCost::getScheduleId, id));
            //调整费用
            List<SeriesTripScheduleCustomFeeAdjust> customFeeAdjustList = customFeeAdjustService.list(new QueryWrapper<SeriesTripScheduleCustomFeeAdjust>().lambda().eq(SeriesTripScheduleCustomFeeAdjust::getCostId, seriesTripCustomCost.getId()));
            if (CollectionUtils.isNotEmpty(customFeeAdjustList)) {
                for (SeriesTripScheduleCustomFeeAdjust seriesTripScheduleCustomFeeAdjust : customFeeAdjustList) {
                    FeeAdjustSaveReq feeAdjustSaveReq = new FeeAdjustSaveReq();
//                	feeAdjustSaveReq.setCostAmount(seriesTripCustomFeeAdjustTouristType.getPrice());
                    feeAdjustList.add(feeAdjustSaveReq);
                    List<FeeAdjustSavePriceReq> currencyList = new ArrayList<>();
                    feeAdjustSaveReq.setCurrencyList(currencyList);

                    List<SeriesTripScheduleCustomAdjustTourist> customFeeAdjustTouristTypeList = customAdjustTouristService.list(new QueryWrapper<SeriesTripScheduleCustomAdjustTourist>().lambda().eq(SeriesTripScheduleCustomAdjustTourist::getAdjustId, seriesTripScheduleCustomFeeAdjust.getId()));
                    for (SeriesTripScheduleCustomAdjustTourist seriesTripCustomFeeAdjustTouristType : customFeeAdjustTouristTypeList) {
                        boolean flag = true;

                        BeanUtils.copyProperties(seriesTripScheduleCustomFeeAdjust, feeAdjustSaveReq);

//                        feeAdjustSaveReq.setCurrency(currency);
                        //    feeAdjustSaveReq.setCostAmount(feeAdjust.getCostAmount().multiply(new BigDecimal(feeAdjust.getExchangeRate())));
                        SeriesTripScheduleCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomFeeAdjustTouristType.getDateSubsectionId());
                        if (seriesTripCustomDateSubsection != null) {
                            Integer dateType = seriesTripCustomDateSubsection.getDateType();
                            LocalDate departureDate = tripSchedule.getDepartureDate();
                            if (dateType == 1) {
                                Integer dayOfWeek = departureDate.getDayOfWeek().getValue();
                                List<Integer> weekList = seriesTripCustomDateSubsection.getCheckWeekDays();
                                if (!weekList.contains(dayOfWeek)) {
                                    flag = false;
                                }
                            }
                            if (dateType == 2) {
                                LocalDate dateBegin = seriesTripCustomDateSubsection.getDateBegin();
                                LocalDate dateEnd = seriesTripCustomDateSubsection.getDateEnd();
                                if (departureDate.isAfter(dateBegin) || departureDate.isBefore(dateEnd)) {
                                    flag = false;
                                }

                            }
                        }
                        if (flag) {
                            FeeAdjustSavePriceReq currency = new FeeAdjustSavePriceReq();
                            currency.setCurrencyId(seriesTripScheduleCustomFeeAdjust.getCurrencyId());
                            currency.setCurrency(seriesTripScheduleCustomFeeAdjust.getCurrency());
                            currency.setExchangeRate(seriesTripScheduleCustomFeeAdjust.getExchangeRate());
                            currency.setCostAmount(seriesTripCustomFeeAdjustTouristType.getPrice());
                            currency.setCostStandardAmount(seriesTripCustomFeeAdjustTouristType.getPrice().multiply(new BigDecimal(seriesTripScheduleCustomFeeAdjust.getExchangeRate())));
                            currency.setTripTouristTypeId(seriesTripCustomFeeAdjustTouristType.getTripTouristTypeId());

                            if (currency.getTripTouristTypeId() != null) {
                                MainDataTouristTypeListDTO mainDataTouristTypeListDTO = mainDataTouristTypeMap.get(currency.getTripTouristTypeId());
                                if (mainDataTouristTypeListDTO != null) {
                                    currency.setTripTouristTypeName(mainDataTouristTypeListDTO.getTouristTypeName());
                                }
                            }
                            currencyList.add(currency);
                        }
                    }
                }
            }
        }
        dto.setFeeAdjustList(feeAdjustList);

        //签证
        //根据团期ID获取签证ID列表
        List<Long> visaIdList = tripVisaMapper.queryVisaIdList(tripSchedule.getTripId());
        if (CollectionUtils.isNotEmpty(visaIdList)) {
            //根据ID列表获取签证详情
            List<TripVisaDTO> visaDTOList = visaService.queryByIdList(visaIdList);
            if (CollectionUtils.isNotEmpty(visaDTOList)) {
                visaDTOList.forEach(visa -> {
                    //计算签证单价
                    if (Objects.equals(visa.getAdjustType(), 1)) {
                        //按金额
                        visa.setSinglePrice(visa.getCostAmount().add(visa.getAmount()));
                    } else if (Objects.equals(visa.getAdjustType(), 2)) {
                        //按百分比
                        BigDecimal singlePrice = visa.getCostAmount().add(visa.getCostAmount().multiply(visa.getPercent())).setScale(2, BigDecimal.ROUND_HALF_UP);
                        visa.setSinglePrice(singlePrice);
                    }
                });
                List<VisaDetailDTO> visaList = EntityUtil.copyList(visaDTOList, VisaDetailDTO.class);
                dto.setVisaList(visaList);
            }
        }

        //更改资料收费
        List<TripScheduleChangeRuleDTO> ruleDTOList = tripScheduleInfoChangeRuleMapper.queryBySchedule(id);
        dto.setRuleDTOList(ruleDTOList);
        return Result.success(dto);
    }

    /**
     * 查询团期状态（内部接口）
     *
     * @param idList
     * @return
     */
    @Override
    public List<TripScheduleGuideDTO> getScheduleList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            throw new BusinessException(SystemError.SYS_402);
        }
        List<TripScheduleGuideDTO> scheduleList = seriesTripScheduleMapper.getScheduleList(idList);
        if (CollectionUtils.isNotEmpty(scheduleList)) {
            //统计已下单人数
            List<ScheduleOrderPeopleDTO> scheduleDTOList = EntityUtil.copyList(scheduleList, ScheduleOrderPeopleDTO.class);
            scheduleDTOList = seriesOrderTouristInfoService.sumOrderPeople(scheduleDTOList);
            scheduleList = EntityUtil.copyList(scheduleDTOList, TripScheduleGuideDTO.class);
            return scheduleList;
        }
        return null;
    }

    /**
     * 查询团期基础信息
     *
     * @return
     */
    @Override
    public SeriesTripScheduleBasicDTO findScheduleBasic(Long scheduleId) {
        validateNotNull(scheduleId);
        return seriesTripScheduleMapper.selectById(SeriesTripScheduleBasicDTO.class, scheduleId);
    }

    /**
     * 推广码-旅行团列表(内部接口)
     *
     * @param idList
     * @return
     */
    @Override
    public List<TripScheduleItocDTO> queryScheduleListByIdList(List<Long> idList, Integer businessType) {
        if (CollectionUtils.isEmpty(idList)) {
            throw new BusinessException(SystemError.SYS_402);
        }
        List<TripScheduleItocDTO> dtoList = new ArrayList<>();
        //批量获取团期列表
        QueryWrapper<SeriesTripSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("depart_type", businessType);
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.in("id", idList);
        List<SeriesTripSchedule> tripScheduleList = seriesTripScheduleMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(tripScheduleList)) {
            dtoList = EntityUtil.copyList(tripScheduleList, TripScheduleItocDTO.class);
        }
        return dtoList;
    }

    /**
     * 查询团期可销售余位数
     *
     * @param id
     * @return
     */
    @Override
    public Integer queryScheduleRemain(Long id) {
        return baseMapper.queryScheduleRemain(id);
    }

    @Override
    @Transactional
    public Result updateScheduleNum(Long id, Integer num) {
        //查收已收客人数
        SeriesTripSchedule seriesTripSchedule = seriesTripScheduleMapper.selectById(id);
        Integer lockedTouristNum = seriesTripSchedule.getLockedTouristNum();
        SeriesTripSchedule tripSchedule = new SeriesTripSchedule();
        Integer total = lockedTouristNum + num;
        tripSchedule.setId(id);
        tripSchedule.setLockedTouristNum(total);
        seriesTripScheduleMapper.updateById(tripSchedule);
        return Result.success();
    }

    /**
     * 扣掉余位数
     *
     * @param id
     * @param num
     */
    @Override
    public Integer substrateTouristNum(Long id, Integer num) {
        SeriesTripScheduleTeamRule teamRule = seriesTripScheduleTeamRuleMapper.selectOne(new QueryWrapper<SeriesTripScheduleTeamRule>().eq("schedule_id", id));
        if (teamRule != null){
            if (Objects.equals(teamRule.getAllowReservedSeats(),1)){
                teamRule.setReservedSeatsNum(teamRule.getReservedSeatsNum() - num);
                return seriesTripScheduleTeamRuleMapper.updateById(teamRule);
            }
        }
        return 1;
    }

    /**
     * 根据团号获取团期ID（内部接口）
     *
     * @param tripNumber
     * @return
     */
    @Override
    public Long queryIdByTripNumber(String tripNumber) {
        return seriesTripScheduleMapper.queryIdByNumber(tripNumber);
    }


    /**
     * 计算产品售价信息 - 线下自营
     *
     * @param productId 产品ID
     * @param userId    用户ID
     * @param amount    金额
     * @return
     */
    public BigDecimal calcSalePriceInfoOfflineSelf(Long productId, Long userId, BigDecimal amount) {
        return calcSalePriceInfoHandle(1, productId, userId, amount);
    }

    /**
     * 计算产品售价信息 - 线下代理
     *
     * @param productId 产品ID
     * @param agentId   代理ID
     * @param amount    金额
     * @return
     */
    public BigDecimal calcSalePriceInfoOfflineAgent(Long productId, Long agentId, BigDecimal amount) {
        return calcSalePriceInfoHandle(2, productId, agentId, amount);
    }

    /**
     * 计算产品售价信息逻辑处理
     *
     * @param strategyType 加幅类型，1：自营；2：代理；3：官方平台；4：第三方平台；
     * @param productId    产品ID
     * @param relId        关联ID，strategyType(1：用户ID；2：代理ID；3：渠道ID；4：渠道ID)
     * @param amount       金额
     * @return
     */
    @Override
    public BigDecimal calcSalePriceInfoHandle(int strategyType, Long productId, Long relId, BigDecimal amount) {
        if (amount == null) {
            return null;
        }

        validateNotNull(strategyType, productId, relId);

        //查询加幅规则
        StrategyRuleAmountDto ruleDto = findSalePriceInfoByStrategyRule(strategyType, relId, productId);
        if (ruleDto == null) {
            return amount;
        }

        //加幅
        amount = StrategyRuleUtils.calcPrice(ruleDto.getAdjustType(), ruleDto.getAmount(), amount);

        return amount;
    }

    /**
     * 获取加幅后的团费
     *
     * @param orderType
     * @param scheduleId
     * @param groupFee
     * @param userId
     * @return
     */
    @Override
    public BigDecimal getAddGroupFee(Integer orderType, Long scheduleId, BigDecimal groupFee, Long userId) {
        //下单类型（0 直客下单，1 代理商下单）
        if (Objects.equals(orderType,0)){
            UserDTO userDTO = sysUserService.queryCacheUser(userId);
            if (userDTO != null){
                Long relationId = userDTO.getDeptId();
                SeriesTripScheduleChannelOfflineSelfCustom entity = seriesTripScheduleChannelOfflineSelfCustomMapper.queryByCondition(relationId,userId,scheduleId);
                if (entity != null){
                    //销售增减类型(1:加 2：减)
                    if (Objects.equals(entity.getAdjustType(),1)){
                        groupFee = groupFee.add(entity.getAmount());
                    }else if (Objects.equals(entity.getAdjustType(),2)){
                        groupFee = groupFee.subtract(entity.getAmount());
                    }
                }
            }
        }else if (Objects.equals(orderType,1)){
            SeriesTripScheduleChannelOfflineSelfCustom entity = seriesTripScheduleChannelOfflineAgentCustomMapper.queryByCondition(userId,scheduleId);
            if (entity != null){
                //销售增减类型(1:加 2：减)
                if (Objects.equals(entity.getAdjustType(),1)){
                    groupFee = groupFee.add(entity.getAmount());
                }else if (Objects.equals(entity.getAdjustType(),2)){
                    groupFee = groupFee.subtract(entity.getAmount());
                }
            }
        }
        return groupFee;
    }

    /**
     * 查询产品售价信息,加幅规则
     *
     * @param strategyType 加幅类型，1：自营；2：代理；3：官方平台；4：第三方平台；
     * @param relId        关联ID，strategyType(1：用户ID；2：代理ID；3：渠道ID；4：渠道ID)
     * @return
     */
    private StrategyRuleAmountDto findSalePriceInfoByStrategyRule(int strategyType, Long relId, Long productId) {
        StrategyRuleAmountDto ruleDto = null;
        //团期
        SeriesTripSchedule entity = seriesTripScheduleMapper.assertExistById(productId);

        //自营
        if (strategyType == 1) {
            //销售渠道禁售
            if (BConst.ONE.equals(entity.getSelfSalePermissionType())) {

            } else if (BConst.TWO.equals(entity.getSelfSalePermissionType())) {  //销售渠道所有
                SeriesTripScheduleChannelOfflineSelfCustom ruleEntity = seriesTripScheduleChannelOfflineSelfCustomMapper.selectOneByKey("schedule_id", productId);
                ruleDto = new StrategyRuleAmountDto(ruleEntity.getAdjustType(), ruleEntity.getAmount());
            } else if (BConst.THREE.equals(entity.getSelfSalePermissionType())) {  //销售渠道指定部门/人员
                List<SeriesTripScheduleChannelOfflineSelfCustom> ruleEntityList = seriesTripScheduleChannelOfflineSelfCustomMapper.selectListByKey("schedule_id", productId);
                ruleDto = findSalePriceInfoByStrategyRuleSelfData(ruleEntityList, relId);
            }
        } else if (strategyType == 2) {  //代理
            //销售渠道禁售
            if (BConst.ONE.equals(entity.getAgentSalePermissionType())) {

            } else if (BConst.TWO.equals(entity.getAgentSalePermissionType())) {  //销售渠道所有
                SeriesTripScheduleChannelOfflineAgentCustom ruleEntity = seriesTripScheduleChannelOfflineAgentCustomMapper.selectOneByKey("schedule_id", productId);
                ruleDto = new StrategyRuleAmountDto(ruleEntity.getAdjustType(), ruleEntity.getAmount());
            } else if (BConst.THREE.equals(entity.getAgentSalePermissionType())) {  //销售渠道指定部门/人员
                List<SeriesTripScheduleChannelOfflineAgentCustom> ruleEntityList = seriesTripScheduleChannelOfflineAgentCustomMapper.selectListByKey("schedule_id", productId);
                SeriesTripScheduleChannelOfflineAgentCustom ruleEntity = Assist.filterFirst(ruleEntityList, item -> relId.equals(item.getAgentId()));
                if (ruleEntity != null) {
                    ruleDto = new StrategyRuleAmountDto(ruleEntity.getAdjustType(), ruleEntity.getAmount());
                }
            }
        } else if (strategyType == 3) {  //官方平台

        } else if (strategyType == 4) {  //第三方平台

        }

        return ruleDto;
    }

    /**
     * 查询产品售价信息，加幅规则，自营数据
     *
     * @param ruleEntityList
     * @param userId
     * @return
     */
    private StrategyRuleAmountDto findSalePriceInfoByStrategyRuleSelfData(List<SeriesTripScheduleChannelOfflineSelfCustom> ruleEntityList, Long userId) {
        if (Assist.isEmpty(ruleEntityList)) {
            return null;
        }

        //用户判断
        SeriesTripScheduleChannelOfflineSelfCustom ruleEntity = Assist.filterFirst(ruleEntityList, item -> userId.equals(item.getRelationId()));
        if (ruleEntity != null) {
            return new StrategyRuleAmountDto(ruleEntity.getAdjustType(), ruleEntity.getAmount());
        }

        //轮询上级部门
        List<Long> deptIdList = sysDepartmentService.queryIdsByUserId(userId);
        if (Assist.isEmpty(deptIdList)) {
            return null;
        }

        for (Long deptId : deptIdList) {
            ruleEntity = Assist.filterFirst(ruleEntityList, item -> deptId.equals(item.getRelationId()));
            if (ruleEntity != null) {
                return new StrategyRuleAmountDto(ruleEntity.getAdjustType(), ruleEntity.getAmount());
            }
        }

        return null;
    }

    /**
     * 查询旅客类型主数据列表
     *
     * @return
     */
    private Map<Long, MainDataTouristTypeListDTO> queryMainDataTouristTypeList() {
        MainDataTouristTypeListReq callReq = new MainDataTouristTypeListReq();
        callReq.setStatus(0);
        List<MainDataTouristTypeListDTO> list = mainDataTouristTypeService.list(callReq).getRecords();
        return Assist.forEachToMap(list, MainDataTouristTypeListDTO::getId);
    }
}
